bfin_wdt.c 12 KB

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