bcm63xx_wdt.c 6.8 KB

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