sama5d4_wdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Driver for Atmel SAMA5D4 Watchdog Timer
  3. *
  4. * Copyright (C) 2015 Atmel Corporation
  5. *
  6. * Licensed under GPLv2.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reboot.h>
  17. #include <linux/watchdog.h>
  18. #include "at91sam9_wdt.h"
  19. /* minimum and maximum watchdog timeout, in seconds */
  20. #define MIN_WDT_TIMEOUT 1
  21. #define MAX_WDT_TIMEOUT 16
  22. #define WDT_DEFAULT_TIMEOUT MAX_WDT_TIMEOUT
  23. #define WDT_SEC2TICKS(s) ((s) ? (((s) << 8) - 1) : 0)
  24. struct sama5d4_wdt {
  25. struct watchdog_device wdd;
  26. void __iomem *reg_base;
  27. u32 mr;
  28. unsigned long last_ping;
  29. };
  30. static int wdt_timeout;
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(wdt_timeout, int, 0);
  33. MODULE_PARM_DESC(wdt_timeout,
  34. "Watchdog timeout in seconds. (default = "
  35. __MODULE_STRING(WDT_DEFAULT_TIMEOUT) ")");
  36. module_param(nowayout, bool, 0);
  37. MODULE_PARM_DESC(nowayout,
  38. "Watchdog cannot be stopped once started (default="
  39. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  40. #define wdt_enabled (!(wdt->mr & AT91_WDT_WDDIS))
  41. #define wdt_read(wdt, field) \
  42. readl_relaxed((wdt)->reg_base + (field))
  43. /* 4 slow clock periods is 4/32768 = 122.07µs*/
  44. #define WDT_DELAY usecs_to_jiffies(123)
  45. static void wdt_write(struct sama5d4_wdt *wdt, u32 field, u32 val)
  46. {
  47. /*
  48. * WDT_CR and WDT_MR must not be modified within three slow clock
  49. * periods following a restart of the watchdog performed by a write
  50. * access in WDT_CR.
  51. */
  52. while (time_before(jiffies, wdt->last_ping + WDT_DELAY))
  53. usleep_range(30, 125);
  54. writel_relaxed(val, wdt->reg_base + field);
  55. wdt->last_ping = jiffies;
  56. }
  57. static void wdt_write_nosleep(struct sama5d4_wdt *wdt, u32 field, u32 val)
  58. {
  59. if (time_before(jiffies, wdt->last_ping + WDT_DELAY))
  60. udelay(123);
  61. writel_relaxed(val, wdt->reg_base + field);
  62. wdt->last_ping = jiffies;
  63. }
  64. static int sama5d4_wdt_start(struct watchdog_device *wdd)
  65. {
  66. struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
  67. wdt->mr &= ~AT91_WDT_WDDIS;
  68. wdt_write(wdt, AT91_WDT_MR, wdt->mr);
  69. return 0;
  70. }
  71. static int sama5d4_wdt_stop(struct watchdog_device *wdd)
  72. {
  73. struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
  74. wdt->mr |= AT91_WDT_WDDIS;
  75. wdt_write(wdt, AT91_WDT_MR, wdt->mr);
  76. return 0;
  77. }
  78. static int sama5d4_wdt_ping(struct watchdog_device *wdd)
  79. {
  80. struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
  81. wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
  82. return 0;
  83. }
  84. static int sama5d4_wdt_set_timeout(struct watchdog_device *wdd,
  85. unsigned int timeout)
  86. {
  87. struct sama5d4_wdt *wdt = watchdog_get_drvdata(wdd);
  88. u32 value = WDT_SEC2TICKS(timeout);
  89. wdt->mr &= ~AT91_WDT_WDV;
  90. wdt->mr |= AT91_WDT_SET_WDV(value);
  91. /*
  92. * WDDIS has to be 0 when updating WDD/WDV. The datasheet states: When
  93. * setting the WDDIS bit, and while it is set, the fields WDV and WDD
  94. * must not be modified.
  95. * If the watchdog is enabled, then the timeout can be updated. Else,
  96. * wait that the user enables it.
  97. */
  98. if (wdt_enabled)
  99. wdt_write(wdt, AT91_WDT_MR, wdt->mr & ~AT91_WDT_WDDIS);
  100. wdd->timeout = timeout;
  101. return 0;
  102. }
  103. static const struct watchdog_info sama5d4_wdt_info = {
  104. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  105. .identity = "Atmel SAMA5D4 Watchdog",
  106. };
  107. static const struct watchdog_ops sama5d4_wdt_ops = {
  108. .owner = THIS_MODULE,
  109. .start = sama5d4_wdt_start,
  110. .stop = sama5d4_wdt_stop,
  111. .ping = sama5d4_wdt_ping,
  112. .set_timeout = sama5d4_wdt_set_timeout,
  113. };
  114. static irqreturn_t sama5d4_wdt_irq_handler(int irq, void *dev_id)
  115. {
  116. struct sama5d4_wdt *wdt = platform_get_drvdata(dev_id);
  117. if (wdt_read(wdt, AT91_WDT_SR)) {
  118. pr_crit("Atmel Watchdog Software Reset\n");
  119. emergency_restart();
  120. pr_crit("Reboot didn't succeed\n");
  121. }
  122. return IRQ_HANDLED;
  123. }
  124. static int of_sama5d4_wdt_init(struct device_node *np, struct sama5d4_wdt *wdt)
  125. {
  126. const char *tmp;
  127. wdt->mr = AT91_WDT_WDDIS;
  128. if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
  129. !strcmp(tmp, "software"))
  130. wdt->mr |= AT91_WDT_WDFIEN;
  131. else
  132. wdt->mr |= AT91_WDT_WDRSTEN;
  133. if (of_property_read_bool(np, "atmel,idle-halt"))
  134. wdt->mr |= AT91_WDT_WDIDLEHLT;
  135. if (of_property_read_bool(np, "atmel,dbg-halt"))
  136. wdt->mr |= AT91_WDT_WDDBGHLT;
  137. return 0;
  138. }
  139. static int sama5d4_wdt_init(struct sama5d4_wdt *wdt)
  140. {
  141. u32 reg;
  142. /*
  143. * When booting and resuming, the bootloader may have changed the
  144. * watchdog configuration.
  145. * If the watchdog is already running, we can safely update it.
  146. * Else, we have to disable it properly.
  147. */
  148. if (wdt_enabled) {
  149. wdt_write_nosleep(wdt, AT91_WDT_MR, wdt->mr);
  150. } else {
  151. reg = wdt_read(wdt, AT91_WDT_MR);
  152. if (!(reg & AT91_WDT_WDDIS))
  153. wdt_write_nosleep(wdt, AT91_WDT_MR,
  154. reg | AT91_WDT_WDDIS);
  155. }
  156. return 0;
  157. }
  158. static int sama5d4_wdt_probe(struct platform_device *pdev)
  159. {
  160. struct watchdog_device *wdd;
  161. struct sama5d4_wdt *wdt;
  162. struct resource *res;
  163. void __iomem *regs;
  164. u32 irq = 0;
  165. u32 timeout;
  166. int ret;
  167. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  168. if (!wdt)
  169. return -ENOMEM;
  170. wdd = &wdt->wdd;
  171. wdd->timeout = WDT_DEFAULT_TIMEOUT;
  172. wdd->info = &sama5d4_wdt_info;
  173. wdd->ops = &sama5d4_wdt_ops;
  174. wdd->min_timeout = MIN_WDT_TIMEOUT;
  175. wdd->max_timeout = MAX_WDT_TIMEOUT;
  176. wdt->last_ping = jiffies;
  177. watchdog_set_drvdata(wdd, wdt);
  178. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  179. regs = devm_ioremap_resource(&pdev->dev, res);
  180. if (IS_ERR(regs))
  181. return PTR_ERR(regs);
  182. wdt->reg_base = regs;
  183. irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  184. if (!irq)
  185. dev_warn(&pdev->dev, "failed to get IRQ from DT\n");
  186. ret = of_sama5d4_wdt_init(pdev->dev.of_node, wdt);
  187. if (ret)
  188. return ret;
  189. if ((wdt->mr & AT91_WDT_WDFIEN) && irq) {
  190. ret = devm_request_irq(&pdev->dev, irq, sama5d4_wdt_irq_handler,
  191. IRQF_SHARED | IRQF_IRQPOLL |
  192. IRQF_NO_SUSPEND, pdev->name, pdev);
  193. if (ret) {
  194. dev_err(&pdev->dev,
  195. "cannot register interrupt handler\n");
  196. return ret;
  197. }
  198. }
  199. watchdog_init_timeout(wdd, wdt_timeout, &pdev->dev);
  200. timeout = WDT_SEC2TICKS(wdd->timeout);
  201. wdt->mr |= AT91_WDT_SET_WDD(WDT_SEC2TICKS(MAX_WDT_TIMEOUT));
  202. wdt->mr |= AT91_WDT_SET_WDV(timeout);
  203. ret = sama5d4_wdt_init(wdt);
  204. if (ret)
  205. return ret;
  206. watchdog_set_nowayout(wdd, nowayout);
  207. ret = watchdog_register_device(wdd);
  208. if (ret) {
  209. dev_err(&pdev->dev, "failed to register watchdog device\n");
  210. return ret;
  211. }
  212. platform_set_drvdata(pdev, wdt);
  213. dev_info(&pdev->dev, "initialized (timeout = %d sec, nowayout = %d)\n",
  214. wdd->timeout, nowayout);
  215. return 0;
  216. }
  217. static int sama5d4_wdt_remove(struct platform_device *pdev)
  218. {
  219. struct sama5d4_wdt *wdt = platform_get_drvdata(pdev);
  220. sama5d4_wdt_stop(&wdt->wdd);
  221. watchdog_unregister_device(&wdt->wdd);
  222. return 0;
  223. }
  224. static const struct of_device_id sama5d4_wdt_of_match[] = {
  225. { .compatible = "atmel,sama5d4-wdt", },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(of, sama5d4_wdt_of_match);
  229. #ifdef CONFIG_PM_SLEEP
  230. static int sama5d4_wdt_resume(struct device *dev)
  231. {
  232. struct sama5d4_wdt *wdt = dev_get_drvdata(dev);
  233. /*
  234. * FIXME: writing MR also pings the watchdog which may not be desired.
  235. * This should only be done when the registers are lost on suspend but
  236. * there is no way to get this information right now.
  237. */
  238. sama5d4_wdt_init(wdt);
  239. return 0;
  240. }
  241. #endif
  242. static SIMPLE_DEV_PM_OPS(sama5d4_wdt_pm_ops, NULL,
  243. sama5d4_wdt_resume);
  244. static struct platform_driver sama5d4_wdt_driver = {
  245. .probe = sama5d4_wdt_probe,
  246. .remove = sama5d4_wdt_remove,
  247. .driver = {
  248. .name = "sama5d4_wdt",
  249. .pm = &sama5d4_wdt_pm_ops,
  250. .of_match_table = sama5d4_wdt_of_match,
  251. }
  252. };
  253. module_platform_driver(sama5d4_wdt_driver);
  254. MODULE_AUTHOR("Atmel Corporation");
  255. MODULE_DESCRIPTION("Atmel SAMA5D4 Watchdog Timer driver");
  256. MODULE_LICENSE("GPL v2");