bcm63xx_wdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #include <linux/bitops.h>
  13. #include <linux/errno.h>
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/reboot.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 int nowayout = WATCHDOG_NOWAYOUT;
  48. module_param(nowayout, int, 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. printk(KERN_CRIT PFX ": 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. printk(KERN_CRIT PFX
  110. ": 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 int bcm63xx_wdt_notify_sys(struct notifier_block *this,
  184. unsigned long code, void *unused)
  185. {
  186. if (code == SYS_DOWN || code == SYS_HALT)
  187. bcm63xx_wdt_pause();
  188. return NOTIFY_DONE;
  189. }
  190. static const struct file_operations bcm63xx_wdt_fops = {
  191. .owner = THIS_MODULE,
  192. .llseek = no_llseek,
  193. .write = bcm63xx_wdt_write,
  194. .unlocked_ioctl = bcm63xx_wdt_ioctl,
  195. .open = bcm63xx_wdt_open,
  196. .release = bcm63xx_wdt_release,
  197. };
  198. static struct miscdevice bcm63xx_wdt_miscdev = {
  199. .minor = WATCHDOG_MINOR,
  200. .name = "watchdog",
  201. .fops = &bcm63xx_wdt_fops,
  202. };
  203. static struct notifier_block bcm63xx_wdt_notifier = {
  204. .notifier_call = bcm63xx_wdt_notify_sys,
  205. };
  206. static int bcm63xx_wdt_probe(struct platform_device *pdev)
  207. {
  208. int ret;
  209. struct resource *r;
  210. setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_timer_tick, 0L);
  211. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  212. if (!r) {
  213. dev_err(&pdev->dev, "failed to get resources\n");
  214. return -ENODEV;
  215. }
  216. bcm63xx_wdt_device.regs = ioremap_nocache(r->start, r->end - r->start);
  217. if (!bcm63xx_wdt_device.regs) {
  218. dev_err(&pdev->dev, "failed to remap I/O resources\n");
  219. return -ENXIO;
  220. }
  221. ret = bcm63xx_timer_register(TIMER_WDT_ID, bcm63xx_wdt_isr, NULL);
  222. if (ret < 0) {
  223. dev_err(&pdev->dev, "failed to register wdt timer isr\n");
  224. goto unmap;
  225. }
  226. if (bcm63xx_wdt_settimeout(wdt_time)) {
  227. bcm63xx_wdt_settimeout(WDT_DEFAULT_TIME);
  228. dev_info(&pdev->dev,
  229. ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
  230. wdt_time);
  231. }
  232. ret = register_reboot_notifier(&bcm63xx_wdt_notifier);
  233. if (ret) {
  234. dev_err(&pdev->dev, "failed to register reboot_notifier\n");
  235. goto unregister_timer;
  236. }
  237. ret = misc_register(&bcm63xx_wdt_miscdev);
  238. if (ret < 0) {
  239. dev_err(&pdev->dev, "failed to register watchdog device\n");
  240. goto unregister_reboot_notifier;
  241. }
  242. dev_info(&pdev->dev, " started, timer margin: %d sec\n",
  243. WDT_DEFAULT_TIME);
  244. return 0;
  245. unregister_reboot_notifier:
  246. unregister_reboot_notifier(&bcm63xx_wdt_notifier);
  247. unregister_timer:
  248. bcm63xx_timer_unregister(TIMER_WDT_ID);
  249. unmap:
  250. iounmap(bcm63xx_wdt_device.regs);
  251. return ret;
  252. }
  253. static int bcm63xx_wdt_remove(struct platform_device *pdev)
  254. {
  255. if (!nowayout)
  256. bcm63xx_wdt_pause();
  257. misc_deregister(&bcm63xx_wdt_miscdev);
  258. iounmap(bcm63xx_wdt_device.regs);
  259. unregister_reboot_notifier(&bcm63xx_wdt_notifier);
  260. bcm63xx_timer_unregister(TIMER_WDT_ID);
  261. return 0;
  262. }
  263. static struct platform_driver bcm63xx_wdt = {
  264. .probe = bcm63xx_wdt_probe,
  265. .remove = bcm63xx_wdt_remove,
  266. .driver = {
  267. .name = "bcm63xx-wdt",
  268. }
  269. };
  270. static int __init bcm63xx_wdt_init(void)
  271. {
  272. return platform_driver_register(&bcm63xx_wdt);
  273. }
  274. static void __exit bcm63xx_wdt_exit(void)
  275. {
  276. platform_driver_unregister(&bcm63xx_wdt);
  277. }
  278. module_init(bcm63xx_wdt_init);
  279. module_exit(bcm63xx_wdt_exit);
  280. MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
  281. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  282. MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
  283. MODULE_LICENSE("GPL");
  284. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  285. MODULE_ALIAS("platform:bcm63xx-wdt");