geodewdt.c 6.3 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. fallthrough;
  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. .compat_ioctl = compat_ptr_ioctl,
  164. .open = geodewdt_open,
  165. .release = geodewdt_release,
  166. };
  167. static struct miscdevice geodewdt_miscdev = {
  168. .minor = WATCHDOG_MINOR,
  169. .name = "watchdog",
  170. .fops = &geodewdt_fops,
  171. };
  172. static int __init geodewdt_probe(struct platform_device *dev)
  173. {
  174. int ret;
  175. wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
  176. if (!wdt_timer) {
  177. pr_err("No timers were available\n");
  178. return -ENODEV;
  179. }
  180. /* Set up the timer */
  181. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
  182. GEODEWDT_SCALE | (3 << 8));
  183. /* Set up comparator 2 to reset when the event fires */
  184. cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
  185. /* Set up the initial timeout */
  186. cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
  187. timeout * GEODEWDT_HZ);
  188. ret = misc_register(&geodewdt_miscdev);
  189. return ret;
  190. }
  191. static int geodewdt_remove(struct platform_device *dev)
  192. {
  193. misc_deregister(&geodewdt_miscdev);
  194. return 0;
  195. }
  196. static void geodewdt_shutdown(struct platform_device *dev)
  197. {
  198. geodewdt_disable();
  199. }
  200. static struct platform_driver geodewdt_driver = {
  201. .remove = geodewdt_remove,
  202. .shutdown = geodewdt_shutdown,
  203. .driver = {
  204. .name = DRV_NAME,
  205. },
  206. };
  207. static int __init geodewdt_init(void)
  208. {
  209. int ret;
  210. geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
  211. -1, NULL, 0);
  212. if (IS_ERR(geodewdt_platform_device))
  213. return PTR_ERR(geodewdt_platform_device);
  214. ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe);
  215. if (ret)
  216. goto err;
  217. return 0;
  218. err:
  219. platform_device_unregister(geodewdt_platform_device);
  220. return ret;
  221. }
  222. static void __exit geodewdt_exit(void)
  223. {
  224. platform_device_unregister(geodewdt_platform_device);
  225. platform_driver_unregister(&geodewdt_driver);
  226. }
  227. module_init(geodewdt_init);
  228. module_exit(geodewdt_exit);
  229. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  230. MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
  231. MODULE_LICENSE("GPL");