mena21_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Watchdog driver for the A21 VME CPU Boards
  3. *
  4. * Copyright (C) 2013 MEN Mikro Elektronik Nuernberg GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/gpio.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/bitops.h>
  22. #define NUM_GPIOS 6
  23. enum a21_wdt_gpios {
  24. GPIO_WD_ENAB,
  25. GPIO_WD_FAST,
  26. GPIO_WD_TRIG,
  27. GPIO_WD_RST0,
  28. GPIO_WD_RST1,
  29. GPIO_WD_RST2,
  30. };
  31. struct a21_wdt_drv {
  32. struct watchdog_device wdt;
  33. struct mutex lock;
  34. unsigned gpios[NUM_GPIOS];
  35. };
  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 int a21_wdt_get_bootstatus(struct a21_wdt_drv *drv)
  41. {
  42. int reset = 0;
  43. reset |= gpio_get_value(drv->gpios[GPIO_WD_RST0]) ? (1 << 0) : 0;
  44. reset |= gpio_get_value(drv->gpios[GPIO_WD_RST1]) ? (1 << 1) : 0;
  45. reset |= gpio_get_value(drv->gpios[GPIO_WD_RST2]) ? (1 << 2) : 0;
  46. return reset;
  47. }
  48. static int a21_wdt_start(struct watchdog_device *wdt)
  49. {
  50. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  51. mutex_lock(&drv->lock);
  52. gpio_set_value(drv->gpios[GPIO_WD_ENAB], 1);
  53. mutex_unlock(&drv->lock);
  54. return 0;
  55. }
  56. static int a21_wdt_stop(struct watchdog_device *wdt)
  57. {
  58. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  59. mutex_lock(&drv->lock);
  60. gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
  61. mutex_unlock(&drv->lock);
  62. return 0;
  63. }
  64. static int a21_wdt_ping(struct watchdog_device *wdt)
  65. {
  66. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  67. mutex_lock(&drv->lock);
  68. gpio_set_value(drv->gpios[GPIO_WD_TRIG], 0);
  69. ndelay(10);
  70. gpio_set_value(drv->gpios[GPIO_WD_TRIG], 1);
  71. mutex_unlock(&drv->lock);
  72. return 0;
  73. }
  74. static int a21_wdt_set_timeout(struct watchdog_device *wdt,
  75. unsigned int timeout)
  76. {
  77. struct a21_wdt_drv *drv = watchdog_get_drvdata(wdt);
  78. if (timeout != 1 && timeout != 30) {
  79. dev_err(wdt->dev, "Only 1 and 30 allowed as timeout\n");
  80. return -EINVAL;
  81. }
  82. if (timeout == 30 && wdt->timeout == 1) {
  83. dev_err(wdt->dev,
  84. "Transition from fast to slow mode not allowed\n");
  85. return -EINVAL;
  86. }
  87. mutex_lock(&drv->lock);
  88. if (timeout == 1)
  89. gpio_set_value(drv->gpios[GPIO_WD_FAST], 1);
  90. else
  91. gpio_set_value(drv->gpios[GPIO_WD_FAST], 0);
  92. wdt->timeout = timeout;
  93. mutex_unlock(&drv->lock);
  94. return 0;
  95. }
  96. static const struct watchdog_info a21_wdt_info = {
  97. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  98. .identity = "MEN A21 Watchdog",
  99. };
  100. static const struct watchdog_ops a21_wdt_ops = {
  101. .owner = THIS_MODULE,
  102. .start = a21_wdt_start,
  103. .stop = a21_wdt_stop,
  104. .ping = a21_wdt_ping,
  105. .set_timeout = a21_wdt_set_timeout,
  106. };
  107. static struct watchdog_device a21_wdt = {
  108. .info = &a21_wdt_info,
  109. .ops = &a21_wdt_ops,
  110. .min_timeout = 1,
  111. .max_timeout = 30,
  112. };
  113. static int a21_wdt_probe(struct platform_device *pdev)
  114. {
  115. struct device_node *node;
  116. struct a21_wdt_drv *drv;
  117. unsigned int reset = 0;
  118. int num_gpios;
  119. int ret;
  120. int i;
  121. drv = devm_kzalloc(&pdev->dev, sizeof(struct a21_wdt_drv), GFP_KERNEL);
  122. if (!drv)
  123. return -ENOMEM;
  124. /* Fill GPIO pin array */
  125. node = pdev->dev.of_node;
  126. num_gpios = of_gpio_count(node);
  127. if (num_gpios != NUM_GPIOS) {
  128. dev_err(&pdev->dev, "gpios DT property wrong, got %d want %d",
  129. num_gpios, NUM_GPIOS);
  130. return -ENODEV;
  131. }
  132. for (i = 0; i < num_gpios; i++) {
  133. int val;
  134. val = of_get_gpio(node, i);
  135. if (val < 0)
  136. return val;
  137. drv->gpios[i] = val;
  138. }
  139. /* Request the used GPIOs */
  140. for (i = 0; i < num_gpios; i++) {
  141. ret = devm_gpio_request(&pdev->dev, drv->gpios[i],
  142. "MEN A21 Watchdog");
  143. if (ret)
  144. return ret;
  145. if (i < GPIO_WD_RST0)
  146. ret = gpio_direction_output(drv->gpios[i],
  147. gpio_get_value(drv->gpios[i]));
  148. else /* GPIO_WD_RST[0..2] are inputs */
  149. ret = gpio_direction_input(drv->gpios[i]);
  150. if (ret)
  151. return ret;
  152. }
  153. mutex_init(&drv->lock);
  154. watchdog_init_timeout(&a21_wdt, 30, &pdev->dev);
  155. watchdog_set_nowayout(&a21_wdt, nowayout);
  156. watchdog_set_drvdata(&a21_wdt, drv);
  157. a21_wdt.parent = &pdev->dev;
  158. reset = a21_wdt_get_bootstatus(drv);
  159. if (reset == 2)
  160. a21_wdt.bootstatus |= WDIOF_EXTERN1;
  161. else if (reset == 4)
  162. a21_wdt.bootstatus |= WDIOF_CARDRESET;
  163. else if (reset == 5)
  164. a21_wdt.bootstatus |= WDIOF_POWERUNDER;
  165. else if (reset == 7)
  166. a21_wdt.bootstatus |= WDIOF_EXTERN2;
  167. drv->wdt = a21_wdt;
  168. dev_set_drvdata(&pdev->dev, drv);
  169. ret = watchdog_register_device(&a21_wdt);
  170. if (ret) {
  171. dev_err(&pdev->dev, "Cannot register watchdog device\n");
  172. goto err_register_wd;
  173. }
  174. dev_info(&pdev->dev, "MEN A21 watchdog timer driver enabled\n");
  175. return 0;
  176. err_register_wd:
  177. mutex_destroy(&drv->lock);
  178. return ret;
  179. }
  180. static int a21_wdt_remove(struct platform_device *pdev)
  181. {
  182. struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
  183. dev_warn(&pdev->dev,
  184. "Unregistering A21 watchdog driver, board may reboot\n");
  185. watchdog_unregister_device(&drv->wdt);
  186. mutex_destroy(&drv->lock);
  187. return 0;
  188. }
  189. static void a21_wdt_shutdown(struct platform_device *pdev)
  190. {
  191. struct a21_wdt_drv *drv = dev_get_drvdata(&pdev->dev);
  192. gpio_set_value(drv->gpios[GPIO_WD_ENAB], 0);
  193. }
  194. static const struct of_device_id a21_wdt_ids[] = {
  195. { .compatible = "men,a021-wdt" },
  196. { },
  197. };
  198. MODULE_DEVICE_TABLE(of, a21_wdt_ids);
  199. static struct platform_driver a21_wdt_driver = {
  200. .probe = a21_wdt_probe,
  201. .remove = a21_wdt_remove,
  202. .shutdown = a21_wdt_shutdown,
  203. .driver = {
  204. .name = "a21-watchdog",
  205. .of_match_table = a21_wdt_ids,
  206. },
  207. };
  208. module_platform_driver(a21_wdt_driver);
  209. MODULE_AUTHOR("MEN Mikro Elektronik");
  210. MODULE_DESCRIPTION("MEN A21 Watchdog");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS("platform:a21-watchdog");