geodewdt.c 6.4 KB

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