rdc321x_wdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * RDC321x watchdog driver
  3. *
  4. * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This driver is highly inspired from the cpu5_wdt driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/types.h>
  26. #include <linux/errno.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/fs.h>
  29. #include <linux/ioport.h>
  30. #include <linux/timer.h>
  31. #include <linux/completion.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/watchdog.h>
  35. #include <linux/io.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/mfd/rdc321x.h>
  38. #define RDC_WDT_MASK 0x80000000 /* Mask */
  39. #define RDC_WDT_EN 0x00800000 /* Enable bit */
  40. #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */
  41. #define RDC_WDT_RST 0x00100000 /* Reset bit */
  42. #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */
  43. #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */
  44. #define RDC_WDT_CNT 0x00000001 /* WDT count */
  45. #define RDC_CLS_TMR 0x80003844 /* Clear timer */
  46. #define RDC_WDT_INTERVAL (HZ/10+1)
  47. static int ticks = 1000;
  48. /* some device data */
  49. static struct {
  50. struct completion stop;
  51. int running;
  52. struct timer_list timer;
  53. int queue;
  54. int default_ticks;
  55. unsigned long inuse;
  56. spinlock_t lock;
  57. struct pci_dev *sb_pdev;
  58. int base_reg;
  59. } rdc321x_wdt_device;
  60. /* generic helper functions */
  61. static void rdc321x_wdt_trigger(struct timer_list *unused)
  62. {
  63. unsigned long flags;
  64. u32 val;
  65. if (rdc321x_wdt_device.running)
  66. ticks--;
  67. /* keep watchdog alive */
  68. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  69. pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
  70. rdc321x_wdt_device.base_reg, &val);
  71. val |= RDC_WDT_EN;
  72. pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
  73. rdc321x_wdt_device.base_reg, val);
  74. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  75. /* requeue?? */
  76. if (rdc321x_wdt_device.queue && ticks)
  77. mod_timer(&rdc321x_wdt_device.timer,
  78. jiffies + RDC_WDT_INTERVAL);
  79. else {
  80. /* ticks doesn't matter anyway */
  81. complete(&rdc321x_wdt_device.stop);
  82. }
  83. }
  84. static void rdc321x_wdt_reset(void)
  85. {
  86. ticks = rdc321x_wdt_device.default_ticks;
  87. }
  88. static void rdc321x_wdt_start(void)
  89. {
  90. unsigned long flags;
  91. if (!rdc321x_wdt_device.queue) {
  92. rdc321x_wdt_device.queue = 1;
  93. /* Clear the timer */
  94. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  95. pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
  96. rdc321x_wdt_device.base_reg, RDC_CLS_TMR);
  97. /* Enable watchdog and set the timeout to 81.92 us */
  98. pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
  99. rdc321x_wdt_device.base_reg,
  100. RDC_WDT_EN | RDC_WDT_CNT);
  101. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  102. mod_timer(&rdc321x_wdt_device.timer,
  103. jiffies + RDC_WDT_INTERVAL);
  104. }
  105. /* if process dies, counter is not decremented */
  106. rdc321x_wdt_device.running++;
  107. }
  108. static int rdc321x_wdt_stop(void)
  109. {
  110. if (rdc321x_wdt_device.running)
  111. rdc321x_wdt_device.running = 0;
  112. ticks = rdc321x_wdt_device.default_ticks;
  113. return -EIO;
  114. }
  115. /* filesystem operations */
  116. static int rdc321x_wdt_open(struct inode *inode, struct file *file)
  117. {
  118. if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
  119. return -EBUSY;
  120. return nonseekable_open(inode, file);
  121. }
  122. static int rdc321x_wdt_release(struct inode *inode, struct file *file)
  123. {
  124. clear_bit(0, &rdc321x_wdt_device.inuse);
  125. return 0;
  126. }
  127. static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
  128. unsigned long arg)
  129. {
  130. void __user *argp = (void __user *)arg;
  131. u32 value;
  132. static const struct watchdog_info ident = {
  133. .options = WDIOF_CARDRESET,
  134. .identity = "RDC321x WDT",
  135. };
  136. unsigned long flags;
  137. switch (cmd) {
  138. case WDIOC_KEEPALIVE:
  139. rdc321x_wdt_reset();
  140. break;
  141. case WDIOC_GETSTATUS:
  142. /* Read the value from the DATA register */
  143. spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
  144. pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
  145. rdc321x_wdt_device.base_reg, &value);
  146. spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
  147. if (copy_to_user(argp, &value, sizeof(u32)))
  148. return -EFAULT;
  149. break;
  150. case WDIOC_GETSUPPORT:
  151. if (copy_to_user(argp, &ident, sizeof(ident)))
  152. return -EFAULT;
  153. break;
  154. case WDIOC_SETOPTIONS:
  155. if (copy_from_user(&value, argp, sizeof(int)))
  156. return -EFAULT;
  157. switch (value) {
  158. case WDIOS_ENABLECARD:
  159. rdc321x_wdt_start();
  160. break;
  161. case WDIOS_DISABLECARD:
  162. return rdc321x_wdt_stop();
  163. default:
  164. return -EINVAL;
  165. }
  166. break;
  167. default:
  168. return -ENOTTY;
  169. }
  170. return 0;
  171. }
  172. static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. if (!count)
  176. return -EIO;
  177. rdc321x_wdt_reset();
  178. return count;
  179. }
  180. static const struct file_operations rdc321x_wdt_fops = {
  181. .owner = THIS_MODULE,
  182. .llseek = no_llseek,
  183. .unlocked_ioctl = rdc321x_wdt_ioctl,
  184. .open = rdc321x_wdt_open,
  185. .write = rdc321x_wdt_write,
  186. .release = rdc321x_wdt_release,
  187. };
  188. static struct miscdevice rdc321x_wdt_misc = {
  189. .minor = WATCHDOG_MINOR,
  190. .name = "watchdog",
  191. .fops = &rdc321x_wdt_fops,
  192. };
  193. static int rdc321x_wdt_probe(struct platform_device *pdev)
  194. {
  195. int err;
  196. struct resource *r;
  197. struct rdc321x_wdt_pdata *pdata;
  198. pdata = dev_get_platdata(&pdev->dev);
  199. if (!pdata) {
  200. dev_err(&pdev->dev, "no platform data supplied\n");
  201. return -ENODEV;
  202. }
  203. r = platform_get_resource_byname(pdev, IORESOURCE_IO, "wdt-reg");
  204. if (!r) {
  205. dev_err(&pdev->dev, "failed to get wdt-reg resource\n");
  206. return -ENODEV;
  207. }
  208. rdc321x_wdt_device.sb_pdev = pdata->sb_pdev;
  209. rdc321x_wdt_device.base_reg = r->start;
  210. err = misc_register(&rdc321x_wdt_misc);
  211. if (err < 0) {
  212. dev_err(&pdev->dev, "misc_register failed\n");
  213. return err;
  214. }
  215. spin_lock_init(&rdc321x_wdt_device.lock);
  216. /* Reset the watchdog */
  217. pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
  218. rdc321x_wdt_device.base_reg, RDC_WDT_RST);
  219. init_completion(&rdc321x_wdt_device.stop);
  220. rdc321x_wdt_device.queue = 0;
  221. clear_bit(0, &rdc321x_wdt_device.inuse);
  222. timer_setup(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
  223. rdc321x_wdt_device.default_ticks = ticks;
  224. dev_info(&pdev->dev, "watchdog init success\n");
  225. return 0;
  226. }
  227. static int rdc321x_wdt_remove(struct platform_device *pdev)
  228. {
  229. if (rdc321x_wdt_device.queue) {
  230. rdc321x_wdt_device.queue = 0;
  231. wait_for_completion(&rdc321x_wdt_device.stop);
  232. }
  233. misc_deregister(&rdc321x_wdt_misc);
  234. return 0;
  235. }
  236. static struct platform_driver rdc321x_wdt_driver = {
  237. .probe = rdc321x_wdt_probe,
  238. .remove = rdc321x_wdt_remove,
  239. .driver = {
  240. .name = "rdc321x-wdt",
  241. },
  242. };
  243. module_platform_driver(rdc321x_wdt_driver);
  244. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  245. MODULE_DESCRIPTION("RDC321x watchdog driver");
  246. MODULE_LICENSE("GPL");