geodewdt.c 6.2 KB

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