bfin_wdt.c 11 KB

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