bfin_wdt.c 10 KB

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