bfin_wdt.c 11 KB

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