bfin_wdt.c 12 KB

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