ebc-c384_wdt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <linux/device.h>
  7. #include <linux/dmi.h>
  8. #include <linux/errno.h>
  9. #include <linux/io.h>
  10. #include <linux/ioport.h>
  11. #include <linux/isa.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/watchdog.h>
  17. #define MODULE_NAME "ebc-c384_wdt"
  18. #define WATCHDOG_TIMEOUT 60
  19. /*
  20. * The timeout value in minutes must fit in a single byte when sent to the
  21. * watchdog timer; the maximum timeout possible is 15300 (255 * 60) seconds.
  22. */
  23. #define WATCHDOG_MAX_TIMEOUT 15300
  24. #define BASE_ADDR 0x564
  25. #define ADDR_EXTENT 5
  26. #define CFG_ADDR (BASE_ADDR + 1)
  27. #define PET_ADDR (BASE_ADDR + 2)
  28. static bool nowayout = WATCHDOG_NOWAYOUT;
  29. module_param(nowayout, bool, 0);
  30. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  31. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. static unsigned timeout;
  33. module_param(timeout, uint, 0);
  34. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  35. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  36. static int ebc_c384_wdt_start(struct watchdog_device *wdev)
  37. {
  38. unsigned t = wdev->timeout;
  39. /* resolution is in minutes for timeouts greater than 255 seconds */
  40. if (t > 255)
  41. t = DIV_ROUND_UP(t, 60);
  42. outb(t, PET_ADDR);
  43. return 0;
  44. }
  45. static int ebc_c384_wdt_stop(struct watchdog_device *wdev)
  46. {
  47. outb(0x00, PET_ADDR);
  48. return 0;
  49. }
  50. static int ebc_c384_wdt_set_timeout(struct watchdog_device *wdev, unsigned t)
  51. {
  52. /* resolution is in minutes for timeouts greater than 255 seconds */
  53. if (t > 255) {
  54. /* round second resolution up to minute granularity */
  55. wdev->timeout = roundup(t, 60);
  56. /* set watchdog timer for minutes */
  57. outb(0x00, CFG_ADDR);
  58. } else {
  59. wdev->timeout = t;
  60. /* set watchdog timer for seconds */
  61. outb(0x80, CFG_ADDR);
  62. }
  63. return 0;
  64. }
  65. static const struct watchdog_ops ebc_c384_wdt_ops = {
  66. .start = ebc_c384_wdt_start,
  67. .stop = ebc_c384_wdt_stop,
  68. .set_timeout = ebc_c384_wdt_set_timeout
  69. };
  70. static const struct watchdog_info ebc_c384_wdt_info = {
  71. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
  72. .identity = MODULE_NAME
  73. };
  74. static int ebc_c384_wdt_probe(struct device *dev, unsigned int id)
  75. {
  76. struct watchdog_device *wdd;
  77. if (!devm_request_region(dev, BASE_ADDR, ADDR_EXTENT, dev_name(dev))) {
  78. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  79. BASE_ADDR, BASE_ADDR + ADDR_EXTENT);
  80. return -EBUSY;
  81. }
  82. wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
  83. if (!wdd)
  84. return -ENOMEM;
  85. wdd->info = &ebc_c384_wdt_info;
  86. wdd->ops = &ebc_c384_wdt_ops;
  87. wdd->timeout = WATCHDOG_TIMEOUT;
  88. wdd->min_timeout = 1;
  89. wdd->max_timeout = WATCHDOG_MAX_TIMEOUT;
  90. watchdog_set_nowayout(wdd, nowayout);
  91. watchdog_init_timeout(wdd, timeout, dev);
  92. return devm_watchdog_register_device(dev, wdd);
  93. }
  94. static struct isa_driver ebc_c384_wdt_driver = {
  95. .probe = ebc_c384_wdt_probe,
  96. .driver = {
  97. .name = MODULE_NAME
  98. },
  99. };
  100. static int __init ebc_c384_wdt_init(void)
  101. {
  102. if (!dmi_match(DMI_BOARD_NAME, "EBC-C384 SBC"))
  103. return -ENODEV;
  104. return isa_register_driver(&ebc_c384_wdt_driver, 1);
  105. }
  106. static void __exit ebc_c384_wdt_exit(void)
  107. {
  108. isa_unregister_driver(&ebc_c384_wdt_driver);
  109. }
  110. module_init(ebc_c384_wdt_init);
  111. module_exit(ebc_c384_wdt_exit);
  112. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  113. MODULE_DESCRIPTION("WinSystems EBC-C384 watchdog timer driver");
  114. MODULE_LICENSE("GPL v2");
  115. MODULE_ALIAS("isa:" MODULE_NAME);