Blame view

drivers/watchdog/bfin_wdt.c 10.5 KB
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
1
2
  /*
   * Blackfin On-Chip Watchdog Driver
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
3
4
   *
   * Originally based on softdog.c
3dae93ec3   Mike Frysinger   [WATCHDOG] bfin: ...
5
   * Copyright 2006-2010 Analog Devices Inc.
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
6
   * Copyright 2006-2007 Michele d'Amico
29fa0586d   Alan Cox   [PATCH] Switch al...
7
   * Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *
   * Enter bugs at http://blackfin.uclinux.org/
   *
   * Licensed under the GPL-2 or later.
   */
  
  #include <linux/platform_device.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/types.h>
  #include <linux/timer.h>
  #include <linux/miscdevice.h>
  #include <linux/watchdog.h>
  #include <linux/fs.h>
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
22
23
  #include <linux/init.h>
  #include <linux/interrupt.h>
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
24
  #include <linux/uaccess.h>
089ab0791   Wim Van Sebroeck   [WATCHDOG] Clean-...
25
  #include <asm/blackfin.h>
42bd5d499   Mike Frysinger   watchdog: bfin: u...
26
  #include <asm/bfin_watchdog.h>
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
27

a77dba7e4   Wim Van Sebroeck   [WATCHDOG] Some m...
28
29
30
  #define stamp(fmt, args...) \
  	pr_debug("%s:%i: " fmt "
  ", __func__, __LINE__, ## args)
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
31
  #define stampit() stamp("here i am")
a77dba7e4   Wim Van Sebroeck   [WATCHDOG] Some m...
32
33
34
35
36
37
  #define pr_devinit(fmt, args...) \
  	({ static const __devinitconst char __fmt[] = fmt; \
  	printk(__fmt, ## args); })
  #define pr_init(fmt, args...) \
  	({ static const __initconst char __fmt[] = fmt; \
  	printk(__fmt, ## args); })
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
  #define WATCHDOG_NAME "bfin-wdt"
  #define PFX WATCHDOG_NAME ": "
  
  /* The BF561 has two watchdogs (one per core), but since Linux
   * only runs on core A, we'll just work with that one.
   */
  #ifdef BF561_FAMILY
  # define bfin_read_WDOG_CTL()    bfin_read_WDOGA_CTL()
  # define bfin_read_WDOG_CNT()    bfin_read_WDOGA_CNT()
  # define bfin_read_WDOG_STAT()   bfin_read_WDOGA_STAT()
  # define bfin_write_WDOG_CTL(x)  bfin_write_WDOGA_CTL(x)
  # define bfin_write_WDOG_CNT(x)  bfin_write_WDOGA_CNT(x)
  # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
  #endif
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
53
54
55
56
57
  /* some defaults */
  #define WATCHDOG_TIMEOUT 20
  
  static unsigned int timeout = WATCHDOG_TIMEOUT;
  static int nowayout = WATCHDOG_NOWAYOUT;
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
58
  static const struct watchdog_info bfin_wdt_info;
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
59
60
  static unsigned long open_check;
  static char expect_close;
bf6350a3d   Jiri Slaby   [WATCHDOG] bfin_w...
61
  static DEFINE_SPINLOCK(bfin_wdt_spinlock);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
62
63
64
65
  
  /**
   *	bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
   *
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
66
   *	The Userspace watchdog got a KeepAlive: schedule the next timeout.
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
67
68
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
109
110
111
112
113
114
115
116
117
118
119
   */
  static int bfin_wdt_keepalive(void)
  {
  	stampit();
  	bfin_write_WDOG_STAT(0);
  	return 0;
  }
  
  /**
   *	bfin_wdt_stop - Stop the Watchdog
   *
   *	Stops the on-chip watchdog.
   */
  static int bfin_wdt_stop(void)
  {
  	stampit();
  	bfin_write_WDOG_CTL(WDEN_DISABLE);
  	return 0;
  }
  
  /**
   *	bfin_wdt_start - Start the Watchdog
   *
   *	Starts the on-chip watchdog.  Automatically loads WDOG_CNT
   *	into WDOG_STAT for us.
   */
  static int bfin_wdt_start(void)
  {
  	stampit();
  	bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
  	return 0;
  }
  
  /**
   *	bfin_wdt_running - Check Watchdog status
   *
   *	See if the watchdog is running.
   */
  static int bfin_wdt_running(void)
  {
  	stampit();
  	return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
  }
  
  /**
   *	bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
   *	@t: new timeout value (in seconds)
   *
   *	Translate the specified timeout in seconds into System Clock
   *	terms which is what the on-chip Watchdog requires.
   */
  static int bfin_wdt_set_timeout(unsigned long t)
  {
3dae93ec3   Mike Frysinger   [WATCHDOG] bfin: ...
120
  	u32 cnt, max_t, sclk;
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
121
  	unsigned long flags;
3dae93ec3   Mike Frysinger   [WATCHDOG] bfin: ...
122
123
124
125
  	sclk = get_sclk();
  	max_t = -1 / sclk;
  	cnt = t * sclk;
  	stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
126

3dae93ec3   Mike Frysinger   [WATCHDOG] bfin: ...
127
  	if (t > max_t) {
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
128
129
130
131
132
133
134
135
136
137
  		printk(KERN_WARNING PFX "timeout value is too large
  ");
  		return -EINVAL;
  	}
  
  	spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  	{
  		int run = bfin_wdt_running();
  		bfin_wdt_stop();
  		bfin_write_WDOG_CNT(cnt);
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
138
139
  		if (run)
  			bfin_wdt_start();
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  	}
  	spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  
  	timeout = t;
  
  	return 0;
  }
  
  /**
   *	bfin_wdt_open - Open the Device
   *	@inode: inode of device
   *	@file: file handle of device
   *
   *	Watchdog device is opened and started.
   */
  static int bfin_wdt_open(struct inode *inode, struct file *file)
  {
  	stampit();
  
  	if (test_and_set_bit(0, &open_check))
  		return -EBUSY;
  
  	if (nowayout)
  		__module_get(THIS_MODULE);
  
  	bfin_wdt_keepalive();
  	bfin_wdt_start();
  
  	return nonseekable_open(inode, file);
  }
  
  /**
   *	bfin_wdt_close - Close the Device
   *	@inode: inode of device
   *	@file: file handle of device
   *
   *	Watchdog device is closed and stopped.
   */
  static int bfin_wdt_release(struct inode *inode, struct file *file)
  {
  	stampit();
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
181
  	if (expect_close == 42)
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
182
  		bfin_wdt_stop();
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
183
184
185
186
  	else {
  		printk(KERN_CRIT PFX
  			"Unexpected close, not stopping watchdog!
  ");
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
187
188
  		bfin_wdt_keepalive();
  	}
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
189
190
  	expect_close = 0;
  	clear_bit(0, &open_check);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
191
192
193
194
195
196
197
198
199
200
201
202
203
  	return 0;
  }
  
  /**
   *	bfin_wdt_write - Write to Device
   *	@file: file handle of device
   *	@buf: buffer to write
   *	@count: length of buffer
   *	@ppos: offset
   *
   *	Pings the watchdog on write.
   */
  static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
204
  						size_t len, loff_t *ppos)
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  {
  	stampit();
  
  	if (len) {
  		if (!nowayout) {
  			size_t i;
  
  			/* In case it was set long ago */
  			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;
  			}
  		}
  		bfin_wdt_keepalive();
  	}
  
  	return len;
  }
  
  /**
   *	bfin_wdt_ioctl - Query Device
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
231
232
233
234
235
236
237
   *	@file: file handle of device
   *	@cmd: watchdog command
   *	@arg: argument
   *
   *	Query basic information from the device or ping it, as outlined by the
   *	watchdog API.
   */
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
238
239
  static long bfin_wdt_ioctl(struct file *file,
  				unsigned int cmd, unsigned long arg)
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
240
241
242
243
244
245
246
  {
  	void __user *argp = (void __user *)arg;
  	int __user *p = argp;
  
  	stampit();
  
  	switch (cmd) {
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
247
248
249
250
  	case WDIOC_GETSUPPORT:
  		if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
  			return -EFAULT;
  		else
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
251
  			return 0;
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
252
253
254
  	case WDIOC_GETSTATUS:
  	case WDIOC_GETBOOTSTATUS:
  		return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
255
256
257
258
259
260
261
262
263
264
265
  	case WDIOC_SETOPTIONS: {
  		unsigned long flags;
  		int options, ret = -EINVAL;
  
  		if (get_user(options, p))
  			return -EFAULT;
  
  		spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  		if (options & WDIOS_DISABLECARD) {
  			bfin_wdt_stop();
  			ret = 0;
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
266
  		}
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
267
268
269
  		if (options & WDIOS_ENABLECARD) {
  			bfin_wdt_start();
  			ret = 0;
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
270
  		}
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
271
272
273
  		spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  		return ret;
  	}
0c06090c9   Wim Van Sebroeck   [WATCHDOG] Coding...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	case WDIOC_KEEPALIVE:
  		bfin_wdt_keepalive();
  		return 0;
  	case WDIOC_SETTIMEOUT: {
  		int new_timeout;
  
  		if (get_user(new_timeout, p))
  			return -EFAULT;
  		if (bfin_wdt_set_timeout(new_timeout))
  			return -EINVAL;
  	}
  	/* Fall */
  	case WDIOC_GETTIMEOUT:
  		return put_user(timeout, p);
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
288
289
  	default:
  		return -ENOTTY;
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
290
291
  	}
  }
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  #ifdef CONFIG_PM
  static int state_before_suspend;
  
  /**
   *	bfin_wdt_suspend - suspend the watchdog
   *	@pdev: device being suspended
   *	@state: requested suspend state
   *
   *	Remember if the watchdog was running and stop it.
   *	TODO: is this even right?  Doesn't seem to be any
   *	      standard in the watchdog world ...
   */
  static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  {
  	stampit();
  
  	state_before_suspend = bfin_wdt_running();
  	bfin_wdt_stop();
  
  	return 0;
  }
  
  /**
   *	bfin_wdt_resume - resume the watchdog
   *	@pdev: device being resumed
   *
   *	If the watchdog was running, turn it back on.
   */
  static int bfin_wdt_resume(struct platform_device *pdev)
  {
  	stampit();
  
  	if (state_before_suspend) {
  		bfin_wdt_set_timeout(timeout);
  		bfin_wdt_start();
  	}
  
  	return 0;
  }
  #else
  # define bfin_wdt_suspend NULL
  # define bfin_wdt_resume NULL
  #endif
b47a166ed   Jan Engelhardt   [WATCHDOG] consti...
335
  static const struct file_operations bfin_wdt_fops = {
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
336
337
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
338
  	.write		= bfin_wdt_write,
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
339
340
341
  	.unlocked_ioctl	= bfin_wdt_ioctl,
  	.open		= bfin_wdt_open,
  	.release	= bfin_wdt_release,
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
342
343
344
345
346
347
348
  };
  
  static struct miscdevice bfin_wdt_miscdev = {
  	.minor    = WATCHDOG_MINOR,
  	.name     = "watchdog",
  	.fops     = &bfin_wdt_fops,
  };
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
349
  static const struct watchdog_info bfin_wdt_info = {
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
350
351
  	.identity = "Blackfin Watchdog",
  	.options  = WDIOF_SETTIMEOUT |
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
352
353
  		    WDIOF_KEEPALIVEPING |
  		    WDIOF_MAGICCLOSE,
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
354
  };
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
355
  /**
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
356
   *	bfin_wdt_probe - Initialize module
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
357
   *
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
358
   *	Registers the misc device.  Actual device
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
359
360
   *	initialization is handled by bfin_wdt_open().
   */
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
361
362
363
  static int __devinit bfin_wdt_probe(struct platform_device *pdev)
  {
  	int ret;
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
364
365
  	ret = misc_register(&bfin_wdt_miscdev);
  	if (ret) {
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
366
367
368
369
  		pr_devinit(KERN_ERR PFX
  			"cannot register miscdev on minor=%d (err=%d)
  ",
  				WATCHDOG_MINOR, ret);
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
370
371
372
373
374
375
376
377
378
379
380
381
382
  		return ret;
  	}
  
  	pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)
  ",
  	       timeout, nowayout);
  
  	return 0;
  }
  
  /**
   *	bfin_wdt_remove - Initialize module
   *
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
383
   *	Unregisters the misc device.  Actual device
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
384
385
386
387
388
   *	deinitialization is handled by bfin_wdt_close().
   */
  static int __devexit bfin_wdt_remove(struct platform_device *pdev)
  {
  	misc_deregister(&bfin_wdt_miscdev);
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
389
390
  	return 0;
  }
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
391
392
393
394
395
396
397
398
399
400
401
  /**
   *	bfin_wdt_shutdown - Soft Shutdown Handler
   *
   *	Handles the soft shutdown event.
   */
  static void bfin_wdt_shutdown(struct platform_device *pdev)
  {
  	stampit();
  
  	bfin_wdt_stop();
  }
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
402
403
404
405
406
  static struct platform_device *bfin_wdt_device;
  
  static struct platform_driver bfin_wdt_driver = {
  	.probe     = bfin_wdt_probe,
  	.remove    = __devexit_p(bfin_wdt_remove),
168b5251a   Wim Van Sebroeck   [WATCHDOG] change...
407
  	.shutdown  = bfin_wdt_shutdown,
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  	.suspend   = bfin_wdt_suspend,
  	.resume    = bfin_wdt_resume,
  	.driver    = {
  		.name  = WATCHDOG_NAME,
  		.owner = THIS_MODULE,
  	},
  };
  
  /**
   *	bfin_wdt_init - Initialize module
   *
   *	Checks the module params and registers the platform device & driver.
   *	Real work is in the platform probe function.
   */
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
422
423
424
425
426
427
428
429
430
431
432
433
434
  static int __init bfin_wdt_init(void)
  {
  	int ret;
  
  	stampit();
  
  	/* Check that the timeout value is within range */
  	if (bfin_wdt_set_timeout(timeout))
  		return -EINVAL;
  
  	/* Since this is an on-chip device and needs no board-specific
  	 * resources, we'll handle all the platform device stuff here.
  	 */
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
435
  	ret = platform_driver_register(&bfin_wdt_driver);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
436
  	if (ret) {
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
437
438
  		pr_init(KERN_ERR PFX "unable to register driver
  ");
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
439
440
  		return ret;
  	}
a77dba7e4   Wim Van Sebroeck   [WATCHDOG] Some m...
441
442
  	bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
  								-1, NULL, 0);
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
443
444
445
446
447
  	if (IS_ERR(bfin_wdt_device)) {
  		pr_init(KERN_ERR PFX "unable to register device
  ");
  		platform_driver_unregister(&bfin_wdt_driver);
  		return PTR_ERR(bfin_wdt_device);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
448
  	}
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
449
450
451
452
453
454
  	return 0;
  }
  
  /**
   *	bfin_wdt_exit - Deinitialize module
   *
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
455
456
   *	Back out the platform device & driver steps.  Real work is in the
   *	platform remove function.
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
457
458
459
   */
  static void __exit bfin_wdt_exit(void)
  {
93539b194   Mike Frysinger   [WATCHDOG] Blackf...
460
461
  	platform_device_unregister(bfin_wdt_device);
  	platform_driver_unregister(&bfin_wdt_driver);
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
462
463
464
465
466
467
468
469
470
471
472
  }
  
  module_init(bfin_wdt_init);
  module_exit(bfin_wdt_exit);
  
  MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
  MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  
  module_param(timeout, uint, 0);
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
473
474
475
  MODULE_PARM_DESC(timeout,
  	"Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
  		__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
1e6d320f4   Bryan Wu   [WATCHDOG] Blackf...
476
477
  
  module_param(nowayout, int, 0);
9a5f50d34   Alan Cox   [WATCHDOG 19/57] ...
478
479
480
  MODULE_PARM_DESC(nowayout,
  	"Watchdog cannot be stopped once started (default="
  		__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");