ux500_wdt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2011-2013
  4. *
  5. * Author: Mathieu Poirier <mathieu.poirier@linaro.org> for ST-Ericsson
  6. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/err.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/platform_data/ux500_wdt.h>
  17. #include <linux/mfd/dbx500-prcmu.h>
  18. #define WATCHDOG_TIMEOUT 600 /* 10 minutes */
  19. #define WATCHDOG_MIN 0
  20. #define WATCHDOG_MAX28 268435 /* 28 bit resolution in ms == 268435.455 s */
  21. #define WATCHDOG_MAX32 4294967 /* 32 bit resolution in ms == 4294967.295 s */
  22. static unsigned int timeout = WATCHDOG_TIMEOUT;
  23. module_param(timeout, uint, 0);
  24. MODULE_PARM_DESC(timeout,
  25. "Watchdog timeout in seconds. default="
  26. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  27. static bool nowayout = WATCHDOG_NOWAYOUT;
  28. module_param(nowayout, bool, 0);
  29. MODULE_PARM_DESC(nowayout,
  30. "Watchdog cannot be stopped once started (default="
  31. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. static int ux500_wdt_start(struct watchdog_device *wdd)
  33. {
  34. return prcmu_enable_a9wdog(PRCMU_WDOG_ALL);
  35. }
  36. static int ux500_wdt_stop(struct watchdog_device *wdd)
  37. {
  38. return prcmu_disable_a9wdog(PRCMU_WDOG_ALL);
  39. }
  40. static int ux500_wdt_keepalive(struct watchdog_device *wdd)
  41. {
  42. return prcmu_kick_a9wdog(PRCMU_WDOG_ALL);
  43. }
  44. static int ux500_wdt_set_timeout(struct watchdog_device *wdd,
  45. unsigned int timeout)
  46. {
  47. ux500_wdt_stop(wdd);
  48. prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000);
  49. ux500_wdt_start(wdd);
  50. return 0;
  51. }
  52. static const struct watchdog_info ux500_wdt_info = {
  53. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  54. .identity = "Ux500 WDT",
  55. .firmware_version = 1,
  56. };
  57. static const struct watchdog_ops ux500_wdt_ops = {
  58. .owner = THIS_MODULE,
  59. .start = ux500_wdt_start,
  60. .stop = ux500_wdt_stop,
  61. .ping = ux500_wdt_keepalive,
  62. .set_timeout = ux500_wdt_set_timeout,
  63. };
  64. static struct watchdog_device ux500_wdt = {
  65. .info = &ux500_wdt_info,
  66. .ops = &ux500_wdt_ops,
  67. .min_timeout = WATCHDOG_MIN,
  68. .max_timeout = WATCHDOG_MAX32,
  69. };
  70. static int ux500_wdt_probe(struct platform_device *pdev)
  71. {
  72. struct device *dev = &pdev->dev;
  73. int ret;
  74. struct ux500_wdt_data *pdata = dev_get_platdata(dev);
  75. if (pdata) {
  76. if (pdata->timeout > 0)
  77. timeout = pdata->timeout;
  78. if (pdata->has_28_bits_resolution)
  79. ux500_wdt.max_timeout = WATCHDOG_MAX28;
  80. }
  81. ux500_wdt.parent = dev;
  82. watchdog_set_nowayout(&ux500_wdt, nowayout);
  83. /* disable auto off on sleep */
  84. prcmu_config_a9wdog(PRCMU_WDOG_CPU1, false);
  85. /* set HW initial value */
  86. prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000);
  87. ret = devm_watchdog_register_device(dev, &ux500_wdt);
  88. if (ret)
  89. return ret;
  90. dev_info(dev, "initialized\n");
  91. return 0;
  92. }
  93. #ifdef CONFIG_PM
  94. static int ux500_wdt_suspend(struct platform_device *pdev,
  95. pm_message_t state)
  96. {
  97. if (watchdog_active(&ux500_wdt)) {
  98. ux500_wdt_stop(&ux500_wdt);
  99. prcmu_config_a9wdog(PRCMU_WDOG_CPU1, true);
  100. prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000);
  101. ux500_wdt_start(&ux500_wdt);
  102. }
  103. return 0;
  104. }
  105. static int ux500_wdt_resume(struct platform_device *pdev)
  106. {
  107. if (watchdog_active(&ux500_wdt)) {
  108. ux500_wdt_stop(&ux500_wdt);
  109. prcmu_config_a9wdog(PRCMU_WDOG_CPU1, false);
  110. prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000);
  111. ux500_wdt_start(&ux500_wdt);
  112. }
  113. return 0;
  114. }
  115. #else
  116. #define ux500_wdt_suspend NULL
  117. #define ux500_wdt_resume NULL
  118. #endif
  119. static struct platform_driver ux500_wdt_driver = {
  120. .probe = ux500_wdt_probe,
  121. .suspend = ux500_wdt_suspend,
  122. .resume = ux500_wdt_resume,
  123. .driver = {
  124. .name = "ux500_wdt",
  125. },
  126. };
  127. module_platform_driver(ux500_wdt_driver);
  128. MODULE_AUTHOR("Jonas Aaberg <jonas.aberg@stericsson.com>");
  129. MODULE_DESCRIPTION("Ux500 Watchdog Driver");
  130. MODULE_LICENSE("GPL");
  131. MODULE_ALIAS("platform:ux500_wdt");