geodewdt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Watchdog timer for machines with the CS5535/CS5536 companion chip
  2. *
  3. * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
  4. * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/types.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/fs.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reboot.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/cs5535.h>
  21. #define GEODEWDT_HZ 500
  22. #define GEODEWDT_SCALE 6
  23. #define GEODEWDT_MAX_SECONDS 131
  24. #define WDT_FLAGS_OPEN 1
  25. #define WDT_FLAGS_ORPHAN 2
  26. #define DRV_NAME "geodewdt"
  27. #define WATCHDOG_NAME "Geode GX/LX WDT"
  28. #define WATCHDOG_TIMEOUT 60
  29. static int timeout = WATCHDOG_TIMEOUT;
  30. module_param(timeout, int, 0);
  31. MODULE_PARM_DESC(timeout,
  32. "Watchdog timeout in seconds. 1<= timeout <=131, default="
  33. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  34. static int nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, int, 0);
  36. MODULE_PARM_DESC(nowayout,
  37. "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. static struct platform_device *geodewdt_platform_device;
  40. static unsigned long wdt_flags;
  41. static struct cs5535_mfgpt_timer *wdt_timer;
  42. static int safe_close;
  43. static void geodewdt_ping(void)
  44. {
  45. /* Stop the counter */
  46. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
  47. /* Reset the counter */
  48. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
  49. /* Enable the counter */
  50. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
  51. }
  52. static void geodewdt_disable(void)
  53. {
  54. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
  55. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
  56. }
  57. static int geodewdt_set_heartbeat(int val)
  58. {
  59. if (val < 1 || val > GEODEWDT_MAX_SECONDS)
  60. return -EINVAL;
  61. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
  62. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
  63. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
  64. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
  65. timeout = val;
  66. return 0;
  67. }
  68. static int geodewdt_open(struct inode *inode, struct file *file)
  69. {
  70. if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
  71. return -EBUSY;
  72. if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
  73. __module_get(THIS_MODULE);
  74. geodewdt_ping();
  75. return nonseekable_open(inode, file);
  76. }
  77. static int geodewdt_release(struct inode *inode, struct file *file)
  78. {
  79. if (safe_close) {
  80. geodewdt_disable();
  81. module_put(THIS_MODULE);
  82. } else {
  83. printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
  84. geodewdt_ping();
  85. set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
  86. }
  87. clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
  88. safe_close = 0;
  89. return 0;
  90. }
  91. static ssize_t geodewdt_write(struct file *file, const char __user *data,
  92. size_t len, loff_t *ppos)
  93. {
  94. if (len) {
  95. if (!nowayout) {
  96. size_t i;
  97. safe_close = 0;
  98. for (i = 0; i != len; i++) {
  99. char c;
  100. if (get_user(c, data + i))
  101. return -EFAULT;
  102. if (c == 'V')
  103. safe_close = 1;
  104. }
  105. }
  106. geodewdt_ping();
  107. }
  108. return len;
  109. }
  110. static long geodewdt_ioctl(struct file *file, unsigned int cmd,
  111. unsigned long arg)
  112. {
  113. void __user *argp = (void __user *)arg;
  114. int __user *p = argp;
  115. int interval;
  116. static const struct watchdog_info ident = {
  117. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
  118. | WDIOF_MAGICCLOSE,
  119. .firmware_version = 1,
  120. .identity = WATCHDOG_NAME,
  121. };
  122. switch (cmd) {
  123. case WDIOC_GETSUPPORT:
  124. return copy_to_user(argp, &ident,
  125. sizeof(ident)) ? -EFAULT : 0;
  126. break;
  127. case WDIOC_GETSTATUS:
  128. case WDIOC_GETBOOTSTATUS:
  129. return put_user(0, p);
  130. case WDIOC_SETOPTIONS:
  131. {
  132. int options, ret = -EINVAL;
  133. if (get_user(options, p))
  134. return -EFAULT;
  135. if (options & WDIOS_DISABLECARD) {
  136. geodewdt_disable();
  137. ret = 0;
  138. }
  139. if (options & WDIOS_ENABLECARD) {
  140. geodewdt_ping();
  141. ret = 0;
  142. }
  143. return ret;
  144. }
  145. case WDIOC_KEEPALIVE:
  146. geodewdt_ping();
  147. return 0;
  148. case WDIOC_SETTIMEOUT:
  149. if (get_user(interval, p))
  150. return -EFAULT;
  151. if (geodewdt_set_heartbeat(interval))
  152. return -EINVAL;
  153. /* Fall through */
  154. case WDIOC_GETTIMEOUT:
  155. return put_user(timeout, p);
  156. default:
  157. return -ENOTTY;
  158. }
  159. return 0;
  160. }
  161. static const struct file_operations geodewdt_fops = {
  162. .owner = THIS_MODULE,
  163. .llseek = no_llseek,
  164. .write = geodewdt_write,
  165. .unlocked_ioctl = geodewdt_ioctl,
  166. .open = geodewdt_open,
  167. .release = geodewdt_release,
  168. };
  169. static struct miscdevice geodewdt_miscdev = {
  170. .minor = WATCHDOG_MINOR,
  171. .name = "watchdog",
  172. .fops = &geodewdt_fops,
  173. };
  174. static int __devinit geodewdt_probe(struct platform_device *dev)
  175. {
  176. int ret;
  177. wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
  178. if (!wdt_timer) {
  179. printk(KERN_ERR "geodewdt: No timers were available\n");
  180. return -ENODEV;
  181. }
  182. /* Set up the timer */
  183. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
  184. GEODEWDT_SCALE | (3 << 8));
  185. /* Set up comparator 2 to reset when the event fires */
  186. cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
  187. /* Set up the initial timeout */
  188. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
  189. timeout * GEODEWDT_HZ);
  190. ret = misc_register(&geodewdt_miscdev);
  191. return ret;
  192. }
  193. static int __devexit geodewdt_remove(struct platform_device *dev)
  194. {
  195. misc_deregister(&geodewdt_miscdev);
  196. return 0;
  197. }
  198. static void geodewdt_shutdown(struct platform_device *dev)
  199. {
  200. geodewdt_disable();
  201. }
  202. static struct platform_driver geodewdt_driver = {
  203. .probe = geodewdt_probe,
  204. .remove = __devexit_p(geodewdt_remove),
  205. .shutdown = geodewdt_shutdown,
  206. .driver = {
  207. .owner = THIS_MODULE,
  208. .name = DRV_NAME,
  209. },
  210. };
  211. static int __init geodewdt_init(void)
  212. {
  213. int ret;
  214. ret = platform_driver_register(&geodewdt_driver);
  215. if (ret)
  216. return ret;
  217. geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
  218. -1, NULL, 0);
  219. if (IS_ERR(geodewdt_platform_device)) {
  220. ret = PTR_ERR(geodewdt_platform_device);
  221. goto err;
  222. }
  223. return 0;
  224. err:
  225. platform_driver_unregister(&geodewdt_driver);
  226. return ret;
  227. }
  228. static void __exit geodewdt_exit(void)
  229. {
  230. platform_device_unregister(geodewdt_platform_device);
  231. platform_driver_unregister(&geodewdt_driver);
  232. }
  233. module_init(geodewdt_init);
  234. module_exit(geodewdt_exit);
  235. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  236. MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
  237. MODULE_LICENSE("GPL");
  238. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);