bfin_wdt.c 11 KB

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