bcm63xx_wdt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/io.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. unsigned long inuse;
  42. atomic_t ticks;
  43. } bcm63xx_wdt_device;
  44. static int expect_close;
  45. static int wdt_time = WDT_DEFAULT_TIME;
  46. static bool nowayout = WATCHDOG_NOWAYOUT;
  47. module_param(nowayout, bool, 0);
  48. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  49. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  50. /* HW functions */
  51. static void bcm63xx_wdt_hw_start(void)
  52. {
  53. bcm_writel(0xfffffffe, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
  54. bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  55. bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  56. }
  57. static void bcm63xx_wdt_hw_stop(void)
  58. {
  59. bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  60. bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
  61. }
  62. static void bcm63xx_wdt_isr(void *data)
  63. {
  64. struct pt_regs *regs = get_irq_regs();
  65. die(PFX " fire", regs);
  66. }
  67. static void bcm63xx_timer_tick(unsigned long unused)
  68. {
  69. if (!atomic_dec_and_test(&bcm63xx_wdt_device.ticks)) {
  70. bcm63xx_wdt_hw_start();
  71. mod_timer(&bcm63xx_wdt_device.timer, jiffies + HZ);
  72. } else
  73. pr_crit("watchdog will restart system\n");
  74. }
  75. static void bcm63xx_wdt_pet(void)
  76. {
  77. atomic_set(&bcm63xx_wdt_device.ticks, wdt_time);
  78. }
  79. static void bcm63xx_wdt_start(void)
  80. {
  81. bcm63xx_wdt_pet();
  82. bcm63xx_timer_tick(0);
  83. }
  84. static void bcm63xx_wdt_pause(void)
  85. {
  86. del_timer_sync(&bcm63xx_wdt_device.timer);
  87. bcm63xx_wdt_hw_stop();
  88. }
  89. static int bcm63xx_wdt_settimeout(int new_time)
  90. {
  91. if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
  92. return -EINVAL;
  93. wdt_time = new_time;
  94. return 0;
  95. }
  96. static int bcm63xx_wdt_open(struct inode *inode, struct file *file)
  97. {
  98. if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse))
  99. return -EBUSY;
  100. bcm63xx_wdt_start();
  101. return nonseekable_open(inode, file);
  102. }
  103. static int bcm63xx_wdt_release(struct inode *inode, struct file *file)
  104. {
  105. if (expect_close == 42)
  106. bcm63xx_wdt_pause();
  107. else {
  108. pr_crit("Unexpected close, not stopping watchdog!\n");
  109. bcm63xx_wdt_start();
  110. }
  111. clear_bit(0, &bcm63xx_wdt_device.inuse);
  112. expect_close = 0;
  113. return 0;
  114. }
  115. static ssize_t bcm63xx_wdt_write(struct file *file, const char *data,
  116. size_t len, loff_t *ppos)
  117. {
  118. if (len) {
  119. if (!nowayout) {
  120. size_t i;
  121. /* In case it was set long ago */
  122. expect_close = 0;
  123. for (i = 0; i != len; i++) {
  124. char c;
  125. if (get_user(c, data + i))
  126. return -EFAULT;
  127. if (c == 'V')
  128. expect_close = 42;
  129. }
  130. }
  131. bcm63xx_wdt_pet();
  132. }
  133. return len;
  134. }
  135. static struct watchdog_info bcm63xx_wdt_info = {
  136. .identity = PFX,
  137. .options = WDIOF_SETTIMEOUT |
  138. WDIOF_KEEPALIVEPING |
  139. WDIOF_MAGICCLOSE,
  140. };
  141. static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd,
  142. unsigned long arg)
  143. {
  144. void __user *argp = (void __user *)arg;
  145. int __user *p = argp;
  146. int new_value, retval = -EINVAL;
  147. switch (cmd) {
  148. case WDIOC_GETSUPPORT:
  149. return copy_to_user(argp, &bcm63xx_wdt_info,
  150. sizeof(bcm63xx_wdt_info)) ? -EFAULT : 0;
  151. case WDIOC_GETSTATUS:
  152. case WDIOC_GETBOOTSTATUS:
  153. return put_user(0, p);
  154. case WDIOC_SETOPTIONS:
  155. if (get_user(new_value, p))
  156. return -EFAULT;
  157. if (new_value & WDIOS_DISABLECARD) {
  158. bcm63xx_wdt_pause();
  159. retval = 0;
  160. }
  161. if (new_value & WDIOS_ENABLECARD) {
  162. bcm63xx_wdt_start();
  163. retval = 0;
  164. }
  165. return retval;
  166. case WDIOC_KEEPALIVE:
  167. bcm63xx_wdt_pet();
  168. return 0;
  169. case WDIOC_SETTIMEOUT:
  170. if (get_user(new_value, p))
  171. return -EFAULT;
  172. if (bcm63xx_wdt_settimeout(new_value))
  173. return -EINVAL;
  174. bcm63xx_wdt_pet();
  175. case WDIOC_GETTIMEOUT:
  176. return put_user(wdt_time, p);
  177. default:
  178. return -ENOTTY;
  179. }
  180. }
  181. static const struct file_operations bcm63xx_wdt_fops = {
  182. .owner = THIS_MODULE,
  183. .llseek = no_llseek,
  184. .write = bcm63xx_wdt_write,
  185. .unlocked_ioctl = bcm63xx_wdt_ioctl,
  186. .open = bcm63xx_wdt_open,
  187. .release = bcm63xx_wdt_release,
  188. };
  189. static struct miscdevice bcm63xx_wdt_miscdev = {
  190. .minor = WATCHDOG_MINOR,
  191. .name = "watchdog",
  192. .fops = &bcm63xx_wdt_fops,
  193. };
  194. static int bcm63xx_wdt_probe(struct platform_device *pdev)
  195. {
  196. int ret;
  197. struct resource *r;
  198. setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_timer_tick, 0L);
  199. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  200. if (!r) {
  201. dev_err(&pdev->dev, "failed to get resources\n");
  202. return -ENODEV;
  203. }
  204. bcm63xx_wdt_device.regs = devm_ioremap_nocache(&pdev->dev, r->start,
  205. 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. return ret;
  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. return ret;
  232. }
  233. static int bcm63xx_wdt_remove(struct platform_device *pdev)
  234. {
  235. if (!nowayout)
  236. bcm63xx_wdt_pause();
  237. misc_deregister(&bcm63xx_wdt_miscdev);
  238. bcm63xx_timer_unregister(TIMER_WDT_ID);
  239. return 0;
  240. }
  241. static void bcm63xx_wdt_shutdown(struct platform_device *pdev)
  242. {
  243. bcm63xx_wdt_pause();
  244. }
  245. static struct platform_driver bcm63xx_wdt_driver = {
  246. .probe = bcm63xx_wdt_probe,
  247. .remove = bcm63xx_wdt_remove,
  248. .shutdown = bcm63xx_wdt_shutdown,
  249. .driver = {
  250. .name = "bcm63xx-wdt",
  251. }
  252. };
  253. module_platform_driver(bcm63xx_wdt_driver);
  254. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  255. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  256. MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
  257. MODULE_LICENSE("GPL");
  258. MODULE_ALIAS("platform:bcm63xx-wdt");