ni903x_wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2016 National Instruments Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/watchdog.h>
  20. #define NIWD_CONTROL 0x01
  21. #define NIWD_COUNTER2 0x02
  22. #define NIWD_COUNTER1 0x03
  23. #define NIWD_COUNTER0 0x04
  24. #define NIWD_SEED2 0x05
  25. #define NIWD_SEED1 0x06
  26. #define NIWD_SEED0 0x07
  27. #define NIWD_IO_SIZE 0x08
  28. #define NIWD_CONTROL_MODE 0x80
  29. #define NIWD_CONTROL_PROC_RESET 0x20
  30. #define NIWD_CONTROL_PET 0x10
  31. #define NIWD_CONTROL_RUNNING 0x08
  32. #define NIWD_CONTROL_CAPTURECOUNTER 0x04
  33. #define NIWD_CONTROL_RESET 0x02
  34. #define NIWD_CONTROL_ALARM 0x01
  35. #define NIWD_PERIOD_NS 30720
  36. #define NIWD_MIN_TIMEOUT 1
  37. #define NIWD_MAX_TIMEOUT 515
  38. #define NIWD_DEFAULT_TIMEOUT 60
  39. #define NIWD_NAME "ni903x_wdt"
  40. struct ni903x_wdt {
  41. struct device *dev;
  42. u16 io_base;
  43. struct watchdog_device wdd;
  44. };
  45. static unsigned int timeout;
  46. module_param(timeout, uint, 0);
  47. MODULE_PARM_DESC(timeout,
  48. "Watchdog timeout in seconds. (default="
  49. __MODULE_STRING(NIWD_DEFAULT_TIMEOUT) ")");
  50. static int nowayout = WATCHDOG_NOWAYOUT;
  51. module_param(nowayout, int, S_IRUGO);
  52. MODULE_PARM_DESC(nowayout,
  53. "Watchdog cannot be stopped once started (default="
  54. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  55. static void ni903x_start(struct ni903x_wdt *wdt)
  56. {
  57. u8 control = inb(wdt->io_base + NIWD_CONTROL);
  58. outb(control | NIWD_CONTROL_RESET, wdt->io_base + NIWD_CONTROL);
  59. outb(control | NIWD_CONTROL_PET, wdt->io_base + NIWD_CONTROL);
  60. }
  61. static int ni903x_wdd_set_timeout(struct watchdog_device *wdd,
  62. unsigned int timeout)
  63. {
  64. struct ni903x_wdt *wdt = watchdog_get_drvdata(wdd);
  65. u32 counter = timeout * (1000000000 / NIWD_PERIOD_NS);
  66. outb(((0x00FF0000 & counter) >> 16), wdt->io_base + NIWD_SEED2);
  67. outb(((0x0000FF00 & counter) >> 8), wdt->io_base + NIWD_SEED1);
  68. outb((0x000000FF & counter), wdt->io_base + NIWD_SEED0);
  69. wdd->timeout = timeout;
  70. return 0;
  71. }
  72. static unsigned int ni903x_wdd_get_timeleft(struct watchdog_device *wdd)
  73. {
  74. struct ni903x_wdt *wdt = watchdog_get_drvdata(wdd);
  75. u8 control, counter0, counter1, counter2;
  76. u32 counter;
  77. control = inb(wdt->io_base + NIWD_CONTROL);
  78. control |= NIWD_CONTROL_CAPTURECOUNTER;
  79. outb(control, wdt->io_base + NIWD_CONTROL);
  80. counter2 = inb(wdt->io_base + NIWD_COUNTER2);
  81. counter1 = inb(wdt->io_base + NIWD_COUNTER1);
  82. counter0 = inb(wdt->io_base + NIWD_COUNTER0);
  83. counter = (counter2 << 16) | (counter1 << 8) | counter0;
  84. return counter / (1000000000 / NIWD_PERIOD_NS);
  85. }
  86. static int ni903x_wdd_ping(struct watchdog_device *wdd)
  87. {
  88. struct ni903x_wdt *wdt = watchdog_get_drvdata(wdd);
  89. u8 control;
  90. control = inb(wdt->io_base + NIWD_CONTROL);
  91. outb(control | NIWD_CONTROL_PET, wdt->io_base + NIWD_CONTROL);
  92. return 0;
  93. }
  94. static int ni903x_wdd_start(struct watchdog_device *wdd)
  95. {
  96. struct ni903x_wdt *wdt = watchdog_get_drvdata(wdd);
  97. outb(NIWD_CONTROL_RESET | NIWD_CONTROL_PROC_RESET,
  98. wdt->io_base + NIWD_CONTROL);
  99. ni903x_wdd_set_timeout(wdd, wdd->timeout);
  100. ni903x_start(wdt);
  101. return 0;
  102. }
  103. static int ni903x_wdd_stop(struct watchdog_device *wdd)
  104. {
  105. struct ni903x_wdt *wdt = watchdog_get_drvdata(wdd);
  106. outb(NIWD_CONTROL_RESET, wdt->io_base + NIWD_CONTROL);
  107. return 0;
  108. }
  109. static acpi_status ni903x_resources(struct acpi_resource *res, void *data)
  110. {
  111. struct ni903x_wdt *wdt = data;
  112. u16 io_size;
  113. switch (res->type) {
  114. case ACPI_RESOURCE_TYPE_IO:
  115. if (wdt->io_base != 0) {
  116. dev_err(wdt->dev, "too many IO resources\n");
  117. return AE_ERROR;
  118. }
  119. wdt->io_base = res->data.io.minimum;
  120. io_size = res->data.io.address_length;
  121. if (io_size < NIWD_IO_SIZE) {
  122. dev_err(wdt->dev, "memory region too small\n");
  123. return AE_ERROR;
  124. }
  125. if (!devm_request_region(wdt->dev, wdt->io_base, io_size,
  126. NIWD_NAME)) {
  127. dev_err(wdt->dev, "failed to get memory region\n");
  128. return AE_ERROR;
  129. }
  130. return AE_OK;
  131. case ACPI_RESOURCE_TYPE_END_TAG:
  132. default:
  133. /* Ignore unsupported resources, e.g. IRQ */
  134. return AE_OK;
  135. }
  136. }
  137. static const struct watchdog_info ni903x_wdd_info = {
  138. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  139. .identity = "NI Watchdog",
  140. };
  141. static const struct watchdog_ops ni903x_wdd_ops = {
  142. .owner = THIS_MODULE,
  143. .start = ni903x_wdd_start,
  144. .stop = ni903x_wdd_stop,
  145. .ping = ni903x_wdd_ping,
  146. .set_timeout = ni903x_wdd_set_timeout,
  147. .get_timeleft = ni903x_wdd_get_timeleft,
  148. };
  149. static int ni903x_acpi_add(struct acpi_device *device)
  150. {
  151. struct device *dev = &device->dev;
  152. struct watchdog_device *wdd;
  153. struct ni903x_wdt *wdt;
  154. acpi_status status;
  155. int ret;
  156. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  157. if (!wdt)
  158. return -ENOMEM;
  159. device->driver_data = wdt;
  160. wdt->dev = dev;
  161. status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  162. ni903x_resources, wdt);
  163. if (ACPI_FAILURE(status) || wdt->io_base == 0) {
  164. dev_err(dev, "failed to get resources\n");
  165. return -ENODEV;
  166. }
  167. wdd = &wdt->wdd;
  168. wdd->info = &ni903x_wdd_info;
  169. wdd->ops = &ni903x_wdd_ops;
  170. wdd->min_timeout = NIWD_MIN_TIMEOUT;
  171. wdd->max_timeout = NIWD_MAX_TIMEOUT;
  172. wdd->timeout = NIWD_DEFAULT_TIMEOUT;
  173. wdd->parent = dev;
  174. watchdog_set_drvdata(wdd, wdt);
  175. watchdog_set_nowayout(wdd, nowayout);
  176. ret = watchdog_init_timeout(wdd, timeout, dev);
  177. if (ret)
  178. dev_err(dev, "unable to set timeout value, using default\n");
  179. ret = watchdog_register_device(wdd);
  180. if (ret) {
  181. dev_err(dev, "failed to register watchdog\n");
  182. return ret;
  183. }
  184. /* Switch from boot mode to user mode */
  185. outb(NIWD_CONTROL_RESET | NIWD_CONTROL_MODE,
  186. wdt->io_base + NIWD_CONTROL);
  187. dev_dbg(dev, "io_base=0x%04X, timeout=%d, nowayout=%d\n",
  188. wdt->io_base, timeout, nowayout);
  189. return 0;
  190. }
  191. static int ni903x_acpi_remove(struct acpi_device *device)
  192. {
  193. struct ni903x_wdt *wdt = acpi_driver_data(device);
  194. ni903x_wdd_stop(&wdt->wdd);
  195. watchdog_unregister_device(&wdt->wdd);
  196. return 0;
  197. }
  198. static const struct acpi_device_id ni903x_device_ids[] = {
  199. {"NIC775C", 0},
  200. {"", 0},
  201. };
  202. MODULE_DEVICE_TABLE(acpi, ni903x_device_ids);
  203. static struct acpi_driver ni903x_acpi_driver = {
  204. .name = NIWD_NAME,
  205. .ids = ni903x_device_ids,
  206. .ops = {
  207. .add = ni903x_acpi_add,
  208. .remove = ni903x_acpi_remove,
  209. },
  210. };
  211. module_acpi_driver(ni903x_acpi_driver);
  212. MODULE_DESCRIPTION("NI 903x Watchdog");
  213. MODULE_AUTHOR("Jeff Westfahl <jeff.westfahl@ni.com>");
  214. MODULE_AUTHOR("Kyle Roeschley <kyle.roeschley@ni.com>");
  215. MODULE_LICENSE("GPL");