bfin_wdt.c 12 KB

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