bcm47xx_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Watchdog driver for Broadcom BCM47XX
  3. *
  4. * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
  5. * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
  6. * Copyright (C) 2012-2013 Hauke Mehrtens <hauke@hauke-m.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/bcm47xx_wdt.h>
  15. #include <linux/bitops.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/reboot.h>
  23. #include <linux/types.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/timer.h>
  26. #include <linux/jiffies.h>
  27. #define DRV_NAME "bcm47xx_wdt"
  28. #define WDT_DEFAULT_TIME 30 /* seconds */
  29. #define WDT_MAX_TIME 255 /* seconds */
  30. static int wdt_time = WDT_DEFAULT_TIME;
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(wdt_time, int, 0);
  33. MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
  34. __MODULE_STRING(WDT_DEFAULT_TIME) ")");
  35. module_param(nowayout, bool, 0);
  36. MODULE_PARM_DESC(nowayout,
  37. "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. static inline struct bcm47xx_wdt *bcm47xx_wdt_get(struct watchdog_device *wdd)
  40. {
  41. return container_of(wdd, struct bcm47xx_wdt, wdd);
  42. }
  43. static void bcm47xx_timer_tick(unsigned long data)
  44. {
  45. struct bcm47xx_wdt *wdt = (struct bcm47xx_wdt *)data;
  46. u32 next_tick = min(wdt->wdd.timeout * 1000, wdt->max_timer_ms);
  47. if (!atomic_dec_and_test(&wdt->soft_ticks)) {
  48. wdt->timer_set_ms(wdt, next_tick);
  49. mod_timer(&wdt->soft_timer, jiffies + HZ);
  50. } else {
  51. pr_crit("Watchdog will fire soon!!!\n");
  52. }
  53. }
  54. static int bcm47xx_wdt_keepalive(struct watchdog_device *wdd)
  55. {
  56. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  57. atomic_set(&wdt->soft_ticks, wdd->timeout);
  58. return 0;
  59. }
  60. static int bcm47xx_wdt_start(struct watchdog_device *wdd)
  61. {
  62. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  63. bcm47xx_wdt_keepalive(wdd);
  64. bcm47xx_timer_tick((unsigned long)wdt);
  65. return 0;
  66. }
  67. static int bcm47xx_wdt_stop(struct watchdog_device *wdd)
  68. {
  69. struct bcm47xx_wdt *wdt = bcm47xx_wdt_get(wdd);
  70. del_timer_sync(&wdt->soft_timer);
  71. wdt->timer_set(wdt, 0);
  72. return 0;
  73. }
  74. static int bcm47xx_wdt_set_timeout(struct watchdog_device *wdd,
  75. unsigned int new_time)
  76. {
  77. if (new_time < 1 || new_time > WDT_MAX_TIME) {
  78. pr_warn("timeout value must be 1<=x<=%d, using %d\n",
  79. WDT_MAX_TIME, new_time);
  80. return -EINVAL;
  81. }
  82. wdd->timeout = new_time;
  83. return 0;
  84. }
  85. static const struct watchdog_info bcm47xx_wdt_info = {
  86. .identity = DRV_NAME,
  87. .options = WDIOF_SETTIMEOUT |
  88. WDIOF_KEEPALIVEPING |
  89. WDIOF_MAGICCLOSE,
  90. };
  91. static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
  92. unsigned long code, void *unused)
  93. {
  94. struct bcm47xx_wdt *wdt;
  95. wdt = container_of(this, struct bcm47xx_wdt, notifier);
  96. if (code == SYS_DOWN || code == SYS_HALT)
  97. wdt->wdd.ops->stop(&wdt->wdd);
  98. return NOTIFY_DONE;
  99. }
  100. static struct watchdog_ops bcm47xx_wdt_ops = {
  101. .owner = THIS_MODULE,
  102. .start = bcm47xx_wdt_start,
  103. .stop = bcm47xx_wdt_stop,
  104. .ping = bcm47xx_wdt_keepalive,
  105. .set_timeout = bcm47xx_wdt_set_timeout,
  106. };
  107. static int bcm47xx_wdt_probe(struct platform_device *pdev)
  108. {
  109. int ret;
  110. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  111. if (!wdt)
  112. return -ENXIO;
  113. setup_timer(&wdt->soft_timer, bcm47xx_timer_tick,
  114. (long unsigned int)wdt);
  115. wdt->wdd.ops = &bcm47xx_wdt_ops;
  116. wdt->wdd.info = &bcm47xx_wdt_info;
  117. wdt->wdd.timeout = WDT_DEFAULT_TIME;
  118. ret = wdt->wdd.ops->set_timeout(&wdt->wdd, timeout);
  119. if (ret)
  120. goto err_timer;
  121. watchdog_set_nowayout(&wdt->wdd, nowayout);
  122. wdt->notifier.notifier_call = &bcm47xx_wdt_notify_sys;
  123. ret = register_reboot_notifier(&wdt->notifier);
  124. if (ret)
  125. goto err_timer;
  126. ret = watchdog_register_device(&wdt->wdd);
  127. if (ret)
  128. goto err_notifier;
  129. pr_info("BCM47xx Watchdog Timer enabled (%d seconds%s)\n",
  130. wdt_time, nowayout ? ", nowayout" : "");
  131. return 0;
  132. err_notifier:
  133. unregister_reboot_notifier(&wdt->notifier);
  134. err_timer:
  135. del_timer_sync(&wdt->soft_timer);
  136. return ret;
  137. }
  138. static int bcm47xx_wdt_remove(struct platform_device *pdev)
  139. {
  140. struct bcm47xx_wdt *wdt = dev_get_platdata(&pdev->dev);
  141. if (!wdt)
  142. return -ENXIO;
  143. watchdog_unregister_device(&wdt->wdd);
  144. unregister_reboot_notifier(&wdt->notifier);
  145. return 0;
  146. }
  147. static struct platform_driver bcm47xx_wdt_driver = {
  148. .driver = {
  149. .owner = THIS_MODULE,
  150. .name = "bcm47xx-wdt",
  151. },
  152. .probe = bcm47xx_wdt_probe,
  153. .remove = bcm47xx_wdt_remove,
  154. };
  155. module_platform_driver(bcm47xx_wdt_driver);
  156. MODULE_AUTHOR("Aleksandar Radovanovic");
  157. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  158. MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
  159. MODULE_LICENSE("GPL");