geodewdt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <asm/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
  64. geodewdt_open(struct inode *inode, struct file *file)
  65. {
  66. if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
  67. return -EBUSY;
  68. if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
  69. __module_get(THIS_MODULE);
  70. geodewdt_ping();
  71. return nonseekable_open(inode, file);
  72. }
  73. static int
  74. geodewdt_release(struct inode *inode, struct file *file)
  75. {
  76. if (safe_close) {
  77. geodewdt_disable();
  78. module_put(THIS_MODULE);
  79. }
  80. else {
  81. printk(KERN_CRIT "Unexpected close - watchdog is not stopping.\n");
  82. geodewdt_ping();
  83. set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
  84. }
  85. clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
  86. safe_close = 0;
  87. return 0;
  88. }
  89. static ssize_t
  90. geodewdt_write(struct file *file, const char __user *data, size_t len,
  91. 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 int
  110. geodewdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  111. unsigned long arg)
  112. {
  113. void __user *argp = (void __user *)arg;
  114. int __user *p = argp;
  115. int interval;
  116. static struct watchdog_info ident = {
  117. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
  118. | WDIOF_MAGICCLOSE,
  119. .firmware_version = 1,
  120. .identity = WATCHDOG_NAME,
  121. };
  122. switch(cmd) {
  123. case WDIOC_GETSUPPORT:
  124. return copy_to_user(argp, &ident,
  125. sizeof(ident)) ? -EFAULT : 0;
  126. break;
  127. case WDIOC_GETSTATUS:
  128. case WDIOC_GETBOOTSTATUS:
  129. return put_user(0, p);
  130. case WDIOC_KEEPALIVE:
  131. geodewdt_ping();
  132. return 0;
  133. case WDIOC_SETTIMEOUT:
  134. if (get_user(interval, p))
  135. return -EFAULT;
  136. if (geodewdt_set_heartbeat(interval))
  137. return -EINVAL;
  138. /* Fall through */
  139. case WDIOC_GETTIMEOUT:
  140. return put_user(timeout, p);
  141. case WDIOC_SETOPTIONS:
  142. {
  143. int options, ret = -EINVAL;
  144. if (get_user(options, p))
  145. return -EFAULT;
  146. if (options & WDIOS_DISABLECARD) {
  147. geodewdt_disable();
  148. ret = 0;
  149. }
  150. if (options & WDIOS_ENABLECARD) {
  151. geodewdt_ping();
  152. ret = 0;
  153. }
  154. return ret;
  155. }
  156. default:
  157. return -ENOTTY;
  158. }
  159. return 0;
  160. }
  161. static const struct file_operations geodewdt_fops = {
  162. .owner = THIS_MODULE,
  163. .llseek = no_llseek,
  164. .write = geodewdt_write,
  165. .ioctl = geodewdt_ioctl,
  166. .open = geodewdt_open,
  167. .release = geodewdt_release,
  168. };
  169. static struct miscdevice geodewdt_miscdev = {
  170. .minor = WATCHDOG_MINOR,
  171. .name = "watchdog",
  172. .fops = &geodewdt_fops
  173. };
  174. static int __devinit
  175. geodewdt_probe(struct platform_device *dev)
  176. {
  177. int ret, timer;
  178. timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
  179. if (timer == -1) {
  180. printk(KERN_ERR "geodewdt: No timers were available\n");
  181. return -ENODEV;
  182. }
  183. wdt_timer = timer;
  184. /* Set up the timer */
  185. geode_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
  186. GEODEWDT_SCALE | (3 << 8));
  187. /* Set up comparator 2 to reset when the event fires */
  188. geode_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
  189. /* Set up the initial timeout */
  190. geode_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
  191. timeout * GEODEWDT_HZ);
  192. ret = misc_register(&geodewdt_miscdev);
  193. return ret;
  194. }
  195. static int __devexit
  196. geodewdt_remove(struct platform_device *dev)
  197. {
  198. misc_deregister(&geodewdt_miscdev);
  199. return 0;
  200. }
  201. static void
  202. geodewdt_shutdown(struct platform_device *dev)
  203. {
  204. geodewdt_disable();
  205. }
  206. static struct platform_driver geodewdt_driver = {
  207. .probe = geodewdt_probe,
  208. .remove = __devexit_p(geodewdt_remove),
  209. .shutdown = geodewdt_shutdown,
  210. .driver = {
  211. .owner = THIS_MODULE,
  212. .name = DRV_NAME,
  213. },
  214. };
  215. static int __init
  216. geodewdt_init(void)
  217. {
  218. int ret;
  219. ret = platform_driver_register(&geodewdt_driver);
  220. if (ret)
  221. return ret;
  222. geodewdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
  223. if (IS_ERR(geodewdt_platform_device)) {
  224. ret = PTR_ERR(geodewdt_platform_device);
  225. goto err;
  226. }
  227. return 0;
  228. err:
  229. platform_driver_unregister(&geodewdt_driver);
  230. return ret;
  231. }
  232. static void __exit
  233. geodewdt_exit(void)
  234. {
  235. platform_device_unregister(geodewdt_platform_device);
  236. platform_driver_unregister(&geodewdt_driver);
  237. }
  238. module_init(geodewdt_init);
  239. module_exit(geodewdt_exit);
  240. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  241. MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
  242. MODULE_LICENSE("GPL");
  243. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);