geodewdt.c 6.5 KB

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