bcm63xx_wdt.c 7.1 KB

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