bfin_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Blackfin On-Chip Watchdog Driver
  3. * Supports BF53[123]/BF53[467]/BF54[2489]/BF561
  4. *
  5. * Originally based on softdog.c
  6. * Copyright 2006-2007 Analog Devices Inc.
  7. * Copyright 2006-2007 Michele d'Amico
  8. * Copyright 1996 Alan Cox <alan@redhat.com>
  9. *
  10. * Enter bugs at http://blackfin.uclinux.org/
  11. *
  12. * Licensed under the GPL-2 or later.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/timer.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/fs.h>
  22. #include <linux/notifier.h>
  23. #include <linux/reboot.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/blackfin.h>
  27. #include <asm/uaccess.h>
  28. #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
  29. #define stampit() stamp("here i am")
  30. #define pr_init(fmt, args...) ({ static const __initdata char __fmt[] = fmt; printk(__fmt, ## args); })
  31. #define WATCHDOG_NAME "bfin-wdt"
  32. #define PFX WATCHDOG_NAME ": "
  33. /* The BF561 has two watchdogs (one per core), but since Linux
  34. * only runs on core A, we'll just work with that one.
  35. */
  36. #ifdef BF561_FAMILY
  37. # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
  38. # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
  39. # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
  40. # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
  41. # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
  42. # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
  43. #endif
  44. /* Bit in SWRST that indicates boot caused by watchdog */
  45. #define SWRST_RESET_WDOG 0x4000
  46. /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
  47. #define WDOG_EXPIRED 0x8000
  48. /* Masks for WDEV field in WDOG_CTL register */
  49. #define ICTL_RESET 0x0
  50. #define ICTL_NMI 0x2
  51. #define ICTL_GPI 0x4
  52. #define ICTL_NONE 0x6
  53. #define ICTL_MASK 0x6
  54. /* Masks for WDEN field in WDOG_CTL register */
  55. #define WDEN_MASK 0x0FF0
  56. #define WDEN_ENABLE 0x0000
  57. #define WDEN_DISABLE 0x0AD0
  58. /* some defaults */
  59. #define WATCHDOG_TIMEOUT 20
  60. static unsigned int timeout = WATCHDOG_TIMEOUT;
  61. static int nowayout = WATCHDOG_NOWAYOUT;
  62. static struct watchdog_info bfin_wdt_info;
  63. static unsigned long open_check;
  64. static char expect_close;
  65. static DEFINE_SPINLOCK(bfin_wdt_spinlock);
  66. /**
  67. * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
  68. *
  69. * The Userspace watchdog got a KeepAlive: schedule the next timeout.
  70. */
  71. static int bfin_wdt_keepalive(void)
  72. {
  73. stampit();
  74. bfin_write_WDOG_STAT(0);
  75. return 0;
  76. }
  77. /**
  78. * bfin_wdt_stop - Stop the Watchdog
  79. *
  80. * Stops the on-chip watchdog.
  81. */
  82. static int bfin_wdt_stop(void)
  83. {
  84. stampit();
  85. bfin_write_WDOG_CTL(WDEN_DISABLE);
  86. return 0;
  87. }
  88. /**
  89. * bfin_wdt_start - Start the Watchdog
  90. *
  91. * Starts the on-chip watchdog. Automatically loads WDOG_CNT
  92. * into WDOG_STAT for us.
  93. */
  94. static int bfin_wdt_start(void)
  95. {
  96. stampit();
  97. bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
  98. return 0;
  99. }
  100. /**
  101. * bfin_wdt_running - Check Watchdog status
  102. *
  103. * See if the watchdog is running.
  104. */
  105. static int bfin_wdt_running(void)
  106. {
  107. stampit();
  108. return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
  109. }
  110. /**
  111. * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
  112. * @t: new timeout value (in seconds)
  113. *
  114. * Translate the specified timeout in seconds into System Clock
  115. * terms which is what the on-chip Watchdog requires.
  116. */
  117. static int bfin_wdt_set_timeout(unsigned long t)
  118. {
  119. u32 cnt;
  120. unsigned long flags;
  121. stampit();
  122. cnt = t * get_sclk();
  123. if (cnt < get_sclk()) {
  124. printk(KERN_WARNING PFX "timeout value is too large\n");
  125. return -EINVAL;
  126. }
  127. spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  128. {
  129. int run = bfin_wdt_running();
  130. bfin_wdt_stop();
  131. bfin_write_WDOG_CNT(cnt);
  132. if (run) bfin_wdt_start();
  133. }
  134. spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  135. timeout = t;
  136. return 0;
  137. }
  138. /**
  139. * bfin_wdt_open - Open the Device
  140. * @inode: inode of device
  141. * @file: file handle of device
  142. *
  143. * Watchdog device is opened and started.
  144. */
  145. static int bfin_wdt_open(struct inode *inode, struct file *file)
  146. {
  147. stampit();
  148. if (test_and_set_bit(0, &open_check))
  149. return -EBUSY;
  150. if (nowayout)
  151. __module_get(THIS_MODULE);
  152. bfin_wdt_keepalive();
  153. bfin_wdt_start();
  154. return nonseekable_open(inode, file);
  155. }
  156. /**
  157. * bfin_wdt_close - Close the Device
  158. * @inode: inode of device
  159. * @file: file handle of device
  160. *
  161. * Watchdog device is closed and stopped.
  162. */
  163. static int bfin_wdt_release(struct inode *inode, struct file *file)
  164. {
  165. stampit();
  166. if (expect_close == 42) {
  167. bfin_wdt_stop();
  168. } else {
  169. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  170. bfin_wdt_keepalive();
  171. }
  172. expect_close = 0;
  173. clear_bit(0, &open_check);
  174. return 0;
  175. }
  176. /**
  177. * bfin_wdt_write - Write to Device
  178. * @file: file handle of device
  179. * @buf: buffer to write
  180. * @count: length of buffer
  181. * @ppos: offset
  182. *
  183. * Pings the watchdog on write.
  184. */
  185. static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
  186. size_t len, loff_t *ppos)
  187. {
  188. stampit();
  189. if (len) {
  190. if (!nowayout) {
  191. size_t i;
  192. /* In case it was set long ago */
  193. expect_close = 0;
  194. for (i = 0; i != len; i++) {
  195. char c;
  196. if (get_user(c, data + i))
  197. return -EFAULT;
  198. if (c == 'V')
  199. expect_close = 42;
  200. }
  201. }
  202. bfin_wdt_keepalive();
  203. }
  204. return len;
  205. }
  206. /**
  207. * bfin_wdt_ioctl - Query Device
  208. * @inode: inode of device
  209. * @file: file handle of device
  210. * @cmd: watchdog command
  211. * @arg: argument
  212. *
  213. * Query basic information from the device or ping it, as outlined by the
  214. * watchdog API.
  215. */
  216. static int bfin_wdt_ioctl(struct inode *inode, struct file *file,
  217. unsigned int cmd, unsigned long arg)
  218. {
  219. void __user *argp = (void __user *)arg;
  220. int __user *p = argp;
  221. stampit();
  222. switch (cmd) {
  223. default:
  224. return -ENOTTY;
  225. case WDIOC_GETSUPPORT:
  226. if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
  227. return -EFAULT;
  228. else
  229. return 0;
  230. case WDIOC_GETSTATUS:
  231. case WDIOC_GETBOOTSTATUS:
  232. return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
  233. case WDIOC_KEEPALIVE:
  234. bfin_wdt_keepalive();
  235. return 0;
  236. case WDIOC_SETTIMEOUT: {
  237. int new_timeout;
  238. if (get_user(new_timeout, p))
  239. return -EFAULT;
  240. if (bfin_wdt_set_timeout(new_timeout))
  241. return -EINVAL;
  242. }
  243. /* Fall */
  244. case WDIOC_GETTIMEOUT:
  245. return put_user(timeout, p);
  246. case WDIOC_SETOPTIONS: {
  247. unsigned long flags;
  248. int options, ret = -EINVAL;
  249. if (get_user(options, p))
  250. return -EFAULT;
  251. spin_lock_irqsave(&bfin_wdt_spinlock, flags);
  252. if (options & WDIOS_DISABLECARD) {
  253. bfin_wdt_stop();
  254. ret = 0;
  255. }
  256. if (options & WDIOS_ENABLECARD) {
  257. bfin_wdt_start();
  258. ret = 0;
  259. }
  260. spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
  261. return ret;
  262. }
  263. }
  264. }
  265. /**
  266. * bfin_wdt_notify_sys - Notifier Handler
  267. * @this: notifier block
  268. * @code: notifier event
  269. * @unused: unused
  270. *
  271. * Handles specific events, such as turning off the watchdog during a
  272. * shutdown event.
  273. */
  274. static int bfin_wdt_notify_sys(struct notifier_block *this, unsigned long code,
  275. void *unused)
  276. {
  277. stampit();
  278. if (code == SYS_DOWN || code == SYS_HALT)
  279. bfin_wdt_stop();
  280. return NOTIFY_DONE;
  281. }
  282. #ifdef CONFIG_PM
  283. static int state_before_suspend;
  284. /**
  285. * bfin_wdt_suspend - suspend the watchdog
  286. * @pdev: device being suspended
  287. * @state: requested suspend state
  288. *
  289. * Remember if the watchdog was running and stop it.
  290. * TODO: is this even right? Doesn't seem to be any
  291. * standard in the watchdog world ...
  292. */
  293. static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  294. {
  295. stampit();
  296. state_before_suspend = bfin_wdt_running();
  297. bfin_wdt_stop();
  298. return 0;
  299. }
  300. /**
  301. * bfin_wdt_resume - resume the watchdog
  302. * @pdev: device being resumed
  303. *
  304. * If the watchdog was running, turn it back on.
  305. */
  306. static int bfin_wdt_resume(struct platform_device *pdev)
  307. {
  308. stampit();
  309. if (state_before_suspend) {
  310. bfin_wdt_set_timeout(timeout);
  311. bfin_wdt_start();
  312. }
  313. return 0;
  314. }
  315. #else
  316. # define bfin_wdt_suspend NULL
  317. # define bfin_wdt_resume NULL
  318. #endif
  319. static struct platform_device bfin_wdt_device = {
  320. .name = WATCHDOG_NAME,
  321. .id = -1,
  322. };
  323. static struct platform_driver bfin_wdt_driver = {
  324. .driver = {
  325. .name = WATCHDOG_NAME,
  326. .owner = THIS_MODULE,
  327. },
  328. .suspend = bfin_wdt_suspend,
  329. .resume = bfin_wdt_resume,
  330. };
  331. static const struct file_operations bfin_wdt_fops = {
  332. .owner = THIS_MODULE,
  333. .llseek = no_llseek,
  334. .write = bfin_wdt_write,
  335. .ioctl = bfin_wdt_ioctl,
  336. .open = bfin_wdt_open,
  337. .release = bfin_wdt_release,
  338. };
  339. static struct miscdevice bfin_wdt_miscdev = {
  340. .minor = WATCHDOG_MINOR,
  341. .name = "watchdog",
  342. .fops = &bfin_wdt_fops,
  343. };
  344. static struct watchdog_info bfin_wdt_info = {
  345. .identity = "Blackfin Watchdog",
  346. .options = WDIOF_SETTIMEOUT |
  347. WDIOF_KEEPALIVEPING |
  348. WDIOF_MAGICCLOSE,
  349. };
  350. static struct notifier_block bfin_wdt_notifier = {
  351. .notifier_call = bfin_wdt_notify_sys,
  352. };
  353. /**
  354. * bfin_wdt_init - Initialize module
  355. *
  356. * Registers the device and notifier handler. Actual device
  357. * initialization is handled by bfin_wdt_open().
  358. */
  359. static int __init bfin_wdt_init(void)
  360. {
  361. int ret;
  362. stampit();
  363. /* Check that the timeout value is within range */
  364. if (bfin_wdt_set_timeout(timeout))
  365. return -EINVAL;
  366. /* Since this is an on-chip device and needs no board-specific
  367. * resources, we'll handle all the platform device stuff here.
  368. */
  369. ret = platform_device_register(&bfin_wdt_device);
  370. if (ret)
  371. return ret;
  372. ret = platform_driver_probe(&bfin_wdt_driver, NULL);
  373. if (ret)
  374. return ret;
  375. ret = register_reboot_notifier(&bfin_wdt_notifier);
  376. if (ret) {
  377. pr_init(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
  378. return ret;
  379. }
  380. ret = misc_register(&bfin_wdt_miscdev);
  381. if (ret) {
  382. pr_init(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  383. WATCHDOG_MINOR, ret);
  384. unregister_reboot_notifier(&bfin_wdt_notifier);
  385. return ret;
  386. }
  387. pr_init(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
  388. timeout, nowayout);
  389. return 0;
  390. }
  391. /**
  392. * bfin_wdt_exit - Deinitialize module
  393. *
  394. * Unregisters the device and notifier handler. Actual device
  395. * deinitialization is handled by bfin_wdt_close().
  396. */
  397. static void __exit bfin_wdt_exit(void)
  398. {
  399. misc_deregister(&bfin_wdt_miscdev);
  400. unregister_reboot_notifier(&bfin_wdt_notifier);
  401. }
  402. module_init(bfin_wdt_init);
  403. module_exit(bfin_wdt_exit);
  404. MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
  405. MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
  406. MODULE_LICENSE("GPL");
  407. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  408. module_param(timeout, uint, 0);
  409. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  410. module_param(nowayout, int, 0);
  411. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");