bcm63xx_wdt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Broadcom BCM63xx SoC watchdog driver
  3. *
  4. * Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com>
  5. * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/errno.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/types.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/timer.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/resource.h>
  29. #include <linux/platform_device.h>
  30. #include <bcm63xx_cpu.h>
  31. #include <bcm63xx_io.h>
  32. #include <bcm63xx_regs.h>
  33. #include <bcm63xx_timer.h>
  34. #define PFX KBUILD_MODNAME
  35. #define WDT_HZ 50000000 /* Fclk */
  36. #define WDT_DEFAULT_TIME 30 /* seconds */
  37. #define WDT_MAX_TIME 256 /* seconds */
  38. static struct {
  39. void __iomem *regs;
  40. struct timer_list timer;
  41. int default_ticks;
  42. unsigned long inuse;
  43. atomic_t ticks;
  44. } bcm63xx_wdt_device;
  45. static int expect_close;
  46. static int wdt_time = WDT_DEFAULT_TIME;
  47. static bool nowayout = WATCHDOG_NOWAYOUT;
  48. module_param(nowayout, bool, 0);
  49. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  50. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  51. /* HW functions */
  52. static void bcm63xx_wdt_hw_start(void)
  53. {
  54. bcm_writel(0xfffffffe, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
  55. bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  56. bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  57. }
  58. static void bcm63xx_wdt_hw_stop(void)
  59. {
  60. bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  61. bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  62. }
  63. static void bcm63xx_wdt_isr(void *data)
  64. {
  65. struct pt_regs *regs = get_irq_regs();
  66. die(PFX " fire", regs);
  67. }
  68. static void bcm63xx_timer_tick(unsigned long unused)
  69. {
  70. if (!atomic_dec_and_test(&bcm63xx_wdt_device.ticks)) {
  71. bcm63xx_wdt_hw_start();
  72. mod_timer(&bcm63xx_wdt_device.timer, jiffies + HZ);
  73. } else
  74. pr_crit("watchdog will restart system\n");
  75. }
  76. static void bcm63xx_wdt_pet(void)
  77. {
  78. atomic_set(&bcm63xx_wdt_device.ticks, wdt_time);
  79. }
  80. static void bcm63xx_wdt_start(void)
  81. {
  82. bcm63xx_wdt_pet();
  83. bcm63xx_timer_tick(0);
  84. }
  85. static void bcm63xx_wdt_pause(void)
  86. {
  87. del_timer_sync(&bcm63xx_wdt_device.timer);
  88. bcm63xx_wdt_hw_stop();
  89. }
  90. static int bcm63xx_wdt_settimeout(int new_time)
  91. {
  92. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  93. return -EINVAL;
  94. wdt_time = new_time;
  95. return 0;
  96. }
  97. static int bcm63xx_wdt_open(struct inode *inode, struct file *file)
  98. {
  99. if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse))
  100. return -EBUSY;
  101. bcm63xx_wdt_start();
  102. return nonseekable_open(inode, file);
  103. }
  104. static int bcm63xx_wdt_release(struct inode *inode, struct file *file)
  105. {
  106. if (expect_close == 42)
  107. bcm63xx_wdt_pause();
  108. else {
  109. pr_crit("Unexpected close, not stopping watchdog!\n");
  110. bcm63xx_wdt_start();
  111. }
  112. clear_bit(0, &bcm63xx_wdt_device.inuse);
  113. expect_close = 0;
  114. return 0;
  115. }
  116. static ssize_t bcm63xx_wdt_write(struct file *file, const char *data,
  117. size_t len, loff_t *ppos)
  118. {
  119. if (len) {
  120. if (!nowayout) {
  121. size_t i;
  122. /* In case it was set long ago */
  123. expect_close = 0;
  124. for (i = 0; i != len; i++) {
  125. char c;
  126. if (get_user(c, data + i))
  127. return -EFAULT;
  128. if (c == 'V')
  129. expect_close = 42;
  130. }
  131. }
  132. bcm63xx_wdt_pet();
  133. }
  134. return len;
  135. }
  136. static struct watchdog_info bcm63xx_wdt_info = {
  137. .identity = PFX,
  138. .options = WDIOF_SETTIMEOUT |
  139. WDIOF_KEEPALIVEPING |
  140. WDIOF_MAGICCLOSE,
  141. };
  142. static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd,
  143. unsigned long arg)
  144. {
  145. void __user *argp = (void __user *)arg;
  146. int __user *p = argp;
  147. int new_value, retval = -EINVAL;
  148. switch (cmd) {
  149. case WDIOC_GETSUPPORT:
  150. return copy_to_user(argp, &bcm63xx_wdt_info,
  151. sizeof(bcm63xx_wdt_info)) ? -EFAULT : 0;
  152. case WDIOC_GETSTATUS:
  153. case WDIOC_GETBOOTSTATUS:
  154. return put_user(0, p);
  155. case WDIOC_SETOPTIONS:
  156. if (get_user(new_value, p))
  157. return -EFAULT;
  158. if (new_value & WDIOS_DISABLECARD) {
  159. bcm63xx_wdt_pause();
  160. retval = 0;
  161. }
  162. if (new_value & WDIOS_ENABLECARD) {
  163. bcm63xx_wdt_start();
  164. retval = 0;
  165. }
  166. return retval;
  167. case WDIOC_KEEPALIVE:
  168. bcm63xx_wdt_pet();
  169. return 0;
  170. case WDIOC_SETTIMEOUT:
  171. if (get_user(new_value, p))
  172. return -EFAULT;
  173. if (bcm63xx_wdt_settimeout(new_value))
  174. return -EINVAL;
  175. bcm63xx_wdt_pet();
  176. case WDIOC_GETTIMEOUT:
  177. return put_user(wdt_time, p);
  178. default:
  179. return -ENOTTY;
  180. }
  181. }
  182. static const struct file_operations bcm63xx_wdt_fops = {
  183. .owner = THIS_MODULE,
  184. .llseek = no_llseek,
  185. .write = bcm63xx_wdt_write,
  186. .unlocked_ioctl = bcm63xx_wdt_ioctl,
  187. .open = bcm63xx_wdt_open,
  188. .release = bcm63xx_wdt_release,
  189. };
  190. static struct miscdevice bcm63xx_wdt_miscdev = {
  191. .minor = WATCHDOG_MINOR,
  192. .name = "watchdog",
  193. .fops = &bcm63xx_wdt_fops,
  194. };
  195. static int __devinit bcm63xx_wdt_probe(struct platform_device *pdev)
  196. {
  197. int ret;
  198. struct resource *r;
  199. setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_timer_tick, 0L);
  200. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  201. if (!r) {
  202. dev_err(&pdev->dev, "failed to get resources\n");
  203. return -ENODEV;
  204. }
  205. bcm63xx_wdt_device.regs = ioremap_nocache(r->start, resource_size(r));
  206. if (!bcm63xx_wdt_device.regs) {
  207. dev_err(&pdev->dev, "failed to remap I/O resources\n");
  208. return -ENXIO;
  209. }
  210. ret = bcm63xx_timer_register(TIMER_WDT_ID, bcm63xx_wdt_isr, NULL);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "failed to register wdt timer isr\n");
  213. goto unmap;
  214. }
  215. if (bcm63xx_wdt_settimeout(wdt_time)) {
  216. bcm63xx_wdt_settimeout(WDT_DEFAULT_TIME);
  217. dev_info(&pdev->dev,
  218. ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
  219. wdt_time);
  220. }
  221. ret = misc_register(&bcm63xx_wdt_miscdev);
  222. if (ret < 0) {
  223. dev_err(&pdev->dev, "failed to register watchdog device\n");
  224. goto unregister_timer;
  225. }
  226. dev_info(&pdev->dev, " started, timer margin: %d sec\n",
  227. WDT_DEFAULT_TIME);
  228. return 0;
  229. unregister_timer:
  230. bcm63xx_timer_unregister(TIMER_WDT_ID);
  231. unmap:
  232. iounmap(bcm63xx_wdt_device.regs);
  233. return ret;
  234. }
  235. static int __devexit bcm63xx_wdt_remove(struct platform_device *pdev)
  236. {
  237. if (!nowayout)
  238. bcm63xx_wdt_pause();
  239. misc_deregister(&bcm63xx_wdt_miscdev);
  240. bcm63xx_timer_unregister(TIMER_WDT_ID);
  241. iounmap(bcm63xx_wdt_device.regs);
  242. return 0;
  243. }
  244. static void bcm63xx_wdt_shutdown(struct platform_device *pdev)
  245. {
  246. bcm63xx_wdt_pause();
  247. }
  248. static struct platform_driver bcm63xx_wdt = {
  249. .probe = bcm63xx_wdt_probe,
  250. .remove = __devexit_p(bcm63xx_wdt_remove),
  251. .shutdown = bcm63xx_wdt_shutdown,
  252. .driver = {
  253. .owner = THIS_MODULE,
  254. .name = "bcm63xx-wdt",
  255. }
  256. };
  257. module_platform_driver(bcm63xx_wdt);
  258. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  259. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  260. MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
  261. MODULE_LICENSE("GPL");
  262. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  263. MODULE_ALIAS("platform:bcm63xx-wdt");