geodewdt.c 6.4 KB

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