ebc-c384_wdt.c 4.2 KB

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