stmp3xxx_wdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Watchdog driver for Freescale STMP37XX/STMP378X
  3. *
  4. * Author: Vitaly Wool <vital@embeddedalley.com>
  5. *
  6. * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
  7. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/fs.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/uaccess.h>
  17. #include <mach/platform.h>
  18. #include <mach/regs-rtc.h>
  19. #define DEFAULT_HEARTBEAT 19
  20. #define MAX_HEARTBEAT (0x10000000 >> 6)
  21. /* missing bitmask in headers */
  22. #define BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER 0x80000000
  23. #define WDT_IN_USE 0
  24. #define WDT_OK_TO_CLOSE 1
  25. #define WDOG_COUNTER_RATE 1000 /* 1 kHz clock */
  26. static DEFINE_SPINLOCK(stmp3xxx_wdt_io_lock);
  27. static unsigned long wdt_status;
  28. static const int nowayout = WATCHDOG_NOWAYOUT;
  29. static int heartbeat = DEFAULT_HEARTBEAT;
  30. static unsigned long boot_status;
  31. static void wdt_enable(u32 value)
  32. {
  33. spin_lock(&stmp3xxx_wdt_io_lock);
  34. __raw_writel(value, REGS_RTC_BASE + HW_RTC_WATCHDOG);
  35. stmp3xxx_setl(BM_RTC_CTRL_WATCHDOGEN, REGS_RTC_BASE + HW_RTC_CTRL);
  36. stmp3xxx_setl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER,
  37. REGS_RTC_BASE + HW_RTC_PERSISTENT1);
  38. spin_unlock(&stmp3xxx_wdt_io_lock);
  39. }
  40. static void wdt_disable(void)
  41. {
  42. spin_lock(&stmp3xxx_wdt_io_lock);
  43. stmp3xxx_clearl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER,
  44. REGS_RTC_BASE + HW_RTC_PERSISTENT1);
  45. stmp3xxx_clearl(BM_RTC_CTRL_WATCHDOGEN, REGS_RTC_BASE + HW_RTC_CTRL);
  46. spin_unlock(&stmp3xxx_wdt_io_lock);
  47. }
  48. static void wdt_ping(void)
  49. {
  50. wdt_enable(heartbeat * WDOG_COUNTER_RATE);
  51. }
  52. static int stmp3xxx_wdt_open(struct inode *inode, struct file *file)
  53. {
  54. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  55. return -EBUSY;
  56. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  57. wdt_ping();
  58. return nonseekable_open(inode, file);
  59. }
  60. static ssize_t stmp3xxx_wdt_write(struct file *file, const char __user *data,
  61. size_t len, loff_t *ppos)
  62. {
  63. if (len) {
  64. if (!nowayout) {
  65. size_t i;
  66. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  67. for (i = 0; i != len; i++) {
  68. char c;
  69. if (get_user(c, data + i))
  70. return -EFAULT;
  71. if (c == 'V')
  72. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  73. }
  74. }
  75. wdt_ping();
  76. }
  77. return len;
  78. }
  79. static const struct watchdog_info ident = {
  80. .options = WDIOF_CARDRESET |
  81. WDIOF_MAGICCLOSE |
  82. WDIOF_SETTIMEOUT |
  83. WDIOF_KEEPALIVEPING,
  84. .identity = "STMP3XXX Watchdog",
  85. };
  86. static long stmp3xxx_wdt_ioctl(struct file *file, unsigned int cmd,
  87. unsigned long arg)
  88. {
  89. void __user *argp = (void __user *)arg;
  90. int __user *p = argp;
  91. int new_heartbeat, opts;
  92. int ret = -ENOTTY;
  93. switch (cmd) {
  94. case WDIOC_GETSUPPORT:
  95. ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  96. break;
  97. case WDIOC_GETSTATUS:
  98. ret = put_user(0, p);
  99. break;
  100. case WDIOC_GETBOOTSTATUS:
  101. ret = put_user(boot_status, p);
  102. break;
  103. case WDIOC_SETOPTIONS:
  104. if (get_user(opts, p)) {
  105. ret = -EFAULT;
  106. break;
  107. }
  108. if (opts & WDIOS_DISABLECARD)
  109. wdt_disable();
  110. else if (opts & WDIOS_ENABLECARD)
  111. wdt_ping();
  112. else {
  113. pr_debug("%s: unknown option 0x%x\n", __func__, opts);
  114. ret = -EINVAL;
  115. break;
  116. }
  117. ret = 0;
  118. break;
  119. case WDIOC_KEEPALIVE:
  120. wdt_ping();
  121. ret = 0;
  122. break;
  123. case WDIOC_SETTIMEOUT:
  124. if (get_user(new_heartbeat, p)) {
  125. ret = -EFAULT;
  126. break;
  127. }
  128. if (new_heartbeat <= 0 || new_heartbeat > MAX_HEARTBEAT) {
  129. ret = -EINVAL;
  130. break;
  131. }
  132. heartbeat = new_heartbeat;
  133. wdt_ping();
  134. /* Fall through */
  135. case WDIOC_GETTIMEOUT:
  136. ret = put_user(heartbeat, p);
  137. break;
  138. }
  139. return ret;
  140. }
  141. static int stmp3xxx_wdt_release(struct inode *inode, struct file *file)
  142. {
  143. int ret = 0;
  144. if (!nowayout) {
  145. if (!test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
  146. wdt_ping();
  147. pr_debug("%s: Device closed unexpectdly\n", __func__);
  148. ret = -EINVAL;
  149. } else {
  150. wdt_disable();
  151. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  152. }
  153. }
  154. clear_bit(WDT_IN_USE, &wdt_status);
  155. return ret;
  156. }
  157. static const struct file_operations stmp3xxx_wdt_fops = {
  158. .owner = THIS_MODULE,
  159. .llseek = no_llseek,
  160. .write = stmp3xxx_wdt_write,
  161. .unlocked_ioctl = stmp3xxx_wdt_ioctl,
  162. .open = stmp3xxx_wdt_open,
  163. .release = stmp3xxx_wdt_release,
  164. };
  165. static struct miscdevice stmp3xxx_wdt_miscdev = {
  166. .minor = WATCHDOG_MINOR,
  167. .name = "watchdog",
  168. .fops = &stmp3xxx_wdt_fops,
  169. };
  170. static int __devinit stmp3xxx_wdt_probe(struct platform_device *pdev)
  171. {
  172. int ret = 0;
  173. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  174. heartbeat = DEFAULT_HEARTBEAT;
  175. boot_status = __raw_readl(REGS_RTC_BASE + HW_RTC_PERSISTENT1) &
  176. BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER;
  177. boot_status = !!boot_status;
  178. stmp3xxx_clearl(BV_RTC_PERSISTENT1_GENERAL__RTC_FORCE_UPDATER,
  179. REGS_RTC_BASE + HW_RTC_PERSISTENT1);
  180. wdt_disable(); /* disable for now */
  181. ret = misc_register(&stmp3xxx_wdt_miscdev);
  182. if (ret < 0) {
  183. dev_err(&pdev->dev, "cannot register misc device\n");
  184. return ret;
  185. }
  186. printk(KERN_INFO "stmp3xxx watchdog: initialized, heartbeat %d sec\n",
  187. heartbeat);
  188. return ret;
  189. }
  190. static int __devexit stmp3xxx_wdt_remove(struct platform_device *pdev)
  191. {
  192. misc_deregister(&stmp3xxx_wdt_miscdev);
  193. return 0;
  194. }
  195. #ifdef CONFIG_PM
  196. static int wdt_suspended;
  197. static u32 wdt_saved_time;
  198. static int stmp3xxx_wdt_suspend(struct platform_device *pdev,
  199. pm_message_t state)
  200. {
  201. if (__raw_readl(REGS_RTC_BASE + HW_RTC_CTRL) &
  202. BM_RTC_CTRL_WATCHDOGEN) {
  203. wdt_suspended = 1;
  204. wdt_saved_time = __raw_readl(REGS_RTC_BASE + HW_RTC_WATCHDOG);
  205. wdt_disable();
  206. }
  207. return 0;
  208. }
  209. static int stmp3xxx_wdt_resume(struct platform_device *pdev)
  210. {
  211. if (wdt_suspended) {
  212. wdt_enable(wdt_saved_time);
  213. wdt_suspended = 0;
  214. }
  215. return 0;
  216. }
  217. #else
  218. #define stmp3xxx_wdt_suspend NULL
  219. #define stmp3xxx_wdt_resume NULL
  220. #endif
  221. static struct platform_driver platform_wdt_driver = {
  222. .driver = {
  223. .name = "stmp3xxx_wdt",
  224. },
  225. .probe = stmp3xxx_wdt_probe,
  226. .remove = __devexit_p(stmp3xxx_wdt_remove),
  227. .suspend = stmp3xxx_wdt_suspend,
  228. .resume = stmp3xxx_wdt_resume,
  229. };
  230. static int __init stmp3xxx_wdt_init(void)
  231. {
  232. return platform_driver_register(&platform_wdt_driver);
  233. }
  234. static void __exit stmp3xxx_wdt_exit(void)
  235. {
  236. return platform_driver_unregister(&platform_wdt_driver);
  237. }
  238. module_init(stmp3xxx_wdt_init);
  239. module_exit(stmp3xxx_wdt_exit);
  240. MODULE_DESCRIPTION("STMP3XXX Watchdog Driver");
  241. MODULE_LICENSE("GPL");
  242. module_param(heartbeat, int, 0);
  243. MODULE_PARM_DESC(heartbeat,
  244. "Watchdog heartbeat period in seconds from 1 to "
  245. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  246. __MODULE_STRING(DEFAULT_HEARTBEAT));
  247. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);