ebc-c384_wdt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Watchdog timer driver for the WinSystems EBC-C384
  4. * Copyright (C) 2016 William Breathitt Gray
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/dmi.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/isa.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/watchdog.h>
  26. #define MODULE_NAME "ebc-c384_wdt"
  27. #define WATCHDOG_TIMEOUT 60
  28. /*
  29. * The timeout value in minutes must fit in a single byte when sent to the
  30. * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds.
  31. */
  32. #define WATCHDOG_MAX_TIMEOUT 15300
  33. #define BASE_ADDR 0x564
  34. #define ADDR_EXTENT 5
  35. #define CFG_ADDR (BASE_ADDR + 1)
  36. #define PET_ADDR (BASE_ADDR + 2)
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. module_param(nowayout, bool, 0);
  39. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  40. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  41. static unsigned timeout;
  42. module_param(timeout, uint, 0);
  43. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  44. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  45. static int ebc_c384_wdt_start(struct watchdog_device *wdev)
  46. {
  47. unsigned t = wdev->timeout;
  48. /* resolution is in minutes for timeouts greater than 255 seconds */
  49. if (t > 255)
  50. t = DIV_ROUND_UP(t, 60);
  51. outb(t, PET_ADDR);
  52. return 0;
  53. }
  54. static int ebc_c384_wdt_stop(struct watchdog_device *wdev)
  55. {
  56. outb(0x00, PET_ADDR);
  57. return 0;
  58. }
  59. static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
  60. {
  61. /* resolution is in minutes for timeouts greater than 255 seconds */
  62. if (t > 255) {
  63. /* round second resolution up to minute granularity */
  64. wdev->timeout = roundup(t, 60);
  65. /* set watchdog timer for minutes */
  66. outb(0x00, CFG_ADDR);
  67. } else {
  68. wdev->timeout = t;
  69. /* set watchdog timer for seconds */
  70. outb(0x80, CFG_ADDR);
  71. }
  72. return 0;
  73. }
  74. static const struct watchdog_ops ebc_c384_wdt_ops = {
  75. .start = ebc_c384_wdt_start,
  76. .stop = ebc_c384_wdt_stop,
  77. .set_timeout = ebc_c384_wdt_set_timeout
  78. };
  79. static const struct watchdog_info ebc_c384_wdt_info = {
  80. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
  81. .identity = MODULE_NAME
  82. };
  83. static int ebc_c384_wdt_probe(struct device *dev, unsigned int id)
  84. {
  85. struct watchdog_device *wdd;
  86. if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) {
  87. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  88. BASE_ADDR, BASE_ADDR + ADDR_EXTENT);
  89. return -EBUSY;
  90. }
  91. wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
  92. if (!wdd)
  93. return -ENOMEM;
  94. wdd->info = &ebc_c384_wdt_info;
  95. wdd->ops = &ebc_c384_wdt_ops;
  96. wdd->timeout = WATCHDOG_TIMEOUT;
  97. wdd->min_timeout = 1;
  98. wdd->max_timeout = WATCHDOG_MAX_TIMEOUT;
  99. watchdog_set_nowayout(wdd, nowayout);
  100. if (watchdog_init_timeout(wdd, timeout, dev))
  101. dev_warn(dev, "Invalid timeout (%u seconds), using default (%u seconds)\n",
  102. timeout, WATCHDOG_TIMEOUT);
  103. return devm_watchdog_register_device(dev, wdd);
  104. }
  105. static struct isa_driver ebc_c384_wdt_driver = {
  106. .probe = ebc_c384_wdt_probe,
  107. .driver = {
  108. .name = MODULE_NAME
  109. },
  110. };
  111. static int __init ebc_c384_wdt_init(void)
  112. {
  113. if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC"))
  114. return -ENODEV;
  115. return isa_register_driver(&ebc_c384_wdt_driver, 1);
  116. }
  117. static void __exit ebc_c384_wdt_exit(void)
  118. {
  119. isa_unregister_driver(&ebc_c384_wdt_driver);
  120. }
  121. module_init(ebc_c384_wdt_init);
  122. module_exit(ebc_c384_wdt_exit);
  123. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  124. MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_ALIAS("isa:" MODULE_NAME);