at91sam9_wdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Watchdog driver for Atmel AT91SAM9x processors.
  3. *
  4. * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
  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; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * The Watchdog Timer Mode Register can be only written to once. If the
  13. * timeout need to be set from Linux, be sure that the bootstrap or the
  14. * bootloader doesn't write to this register.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/reboot.h>
  26. #include <linux/types.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/timer.h>
  30. #include <linux/bitops.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/of.h>
  33. #include <linux/of_irq.h>
  34. #include "at91sam9_wdt.h"
  35. #define DRV_NAME "AT91SAM9 Watchdog"
  36. #define wdt_read(wdt, field) \
  37. __raw_readl((wdt)->base + (field))
  38. #define wdt_write(wtd, field, val) \
  39. __raw_writel((val), (wdt)->base + (field))
  40. /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
  41. * use this to convert a watchdog
  42. * value from/to milliseconds.
  43. */
  44. #define ticks_to_hz_rounddown(t) ((((t) + 1) * HZ) >> 8)
  45. #define ticks_to_hz_roundup(t) (((((t) + 1) * HZ) + 255) >> 8)
  46. #define ticks_to_secs(t) (((t) + 1) >> 8)
  47. #define secs_to_ticks(s) ((s) ? (((s) << 8) - 1) : 0)
  48. #define WDT_MR_RESET 0x3FFF2FFF
  49. /* Watchdog max counter value in ticks */
  50. #define WDT_COUNTER_MAX_TICKS 0xFFF
  51. /* Watchdog max delta/value in secs */
  52. #define WDT_COUNTER_MAX_SECS ticks_to_secs(WDT_COUNTER_MAX_TICKS)
  53. /* Hardware timeout in seconds */
  54. #define WDT_HW_TIMEOUT 2
  55. /* Timer heartbeat (500ms) */
  56. #define WDT_TIMEOUT (HZ/2)
  57. /* User land timeout */
  58. #define WDT_HEARTBEAT 15
  59. static int heartbeat;
  60. module_param(heartbeat, int, 0);
  61. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
  62. "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
  63. static bool nowayout = WATCHDOG_NOWAYOUT;
  64. module_param(nowayout, bool, 0);
  65. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  66. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  67. #define to_wdt(wdd) container_of(wdd, struct at91wdt, wdd)
  68. struct at91wdt {
  69. struct watchdog_device wdd;
  70. void __iomem *base;
  71. unsigned long next_heartbeat; /* the next_heartbeat for the timer */
  72. struct timer_list timer; /* The timer that pings the watchdog */
  73. u32 mr;
  74. u32 mr_mask;
  75. unsigned long heartbeat; /* WDT heartbeat in jiffies */
  76. bool nowayout;
  77. unsigned int irq;
  78. };
  79. /* ......................................................................... */
  80. static irqreturn_t wdt_interrupt(int irq, void *dev_id)
  81. {
  82. struct at91wdt *wdt = (struct at91wdt *)dev_id;
  83. if (wdt_read(wdt, AT91_WDT_SR)) {
  84. pr_crit("at91sam9 WDT software reset\n");
  85. emergency_restart();
  86. pr_crit("Reboot didn't ?????\n");
  87. }
  88. return IRQ_HANDLED;
  89. }
  90. /*
  91. * Reload the watchdog timer. (ie, pat the watchdog)
  92. */
  93. static inline void at91_wdt_reset(struct at91wdt *wdt)
  94. {
  95. wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
  96. }
  97. /*
  98. * Timer tick
  99. */
  100. static void at91_ping(unsigned long data)
  101. {
  102. struct at91wdt *wdt = (struct at91wdt *)data;
  103. if (time_before(jiffies, wdt->next_heartbeat) ||
  104. !watchdog_active(&wdt->wdd)) {
  105. at91_wdt_reset(wdt);
  106. mod_timer(&wdt->timer, jiffies + wdt->heartbeat);
  107. } else {
  108. pr_crit("I will reset your machine !\n");
  109. }
  110. }
  111. static int at91_wdt_start(struct watchdog_device *wdd)
  112. {
  113. struct at91wdt *wdt = to_wdt(wdd);
  114. /* calculate when the next userspace timeout will be */
  115. wdt->next_heartbeat = jiffies + wdd->timeout * HZ;
  116. return 0;
  117. }
  118. static int at91_wdt_stop(struct watchdog_device *wdd)
  119. {
  120. /* The watchdog timer hardware can not be stopped... */
  121. return 0;
  122. }
  123. static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout)
  124. {
  125. wdd->timeout = new_timeout;
  126. return at91_wdt_start(wdd);
  127. }
  128. static int at91_wdt_init(struct platform_device *pdev, struct at91wdt *wdt)
  129. {
  130. u32 tmp;
  131. u32 delta;
  132. u32 value;
  133. int err;
  134. u32 mask = wdt->mr_mask;
  135. unsigned long min_heartbeat = 1;
  136. unsigned long max_heartbeat;
  137. struct device *dev = &pdev->dev;
  138. tmp = wdt_read(wdt, AT91_WDT_MR);
  139. if ((tmp & mask) != (wdt->mr & mask)) {
  140. if (tmp == WDT_MR_RESET) {
  141. wdt_write(wdt, AT91_WDT_MR, wdt->mr);
  142. tmp = wdt_read(wdt, AT91_WDT_MR);
  143. }
  144. }
  145. if (tmp & AT91_WDT_WDDIS) {
  146. if (wdt->mr & AT91_WDT_WDDIS)
  147. return 0;
  148. dev_err(dev, "watchdog is disabled\n");
  149. return -EINVAL;
  150. }
  151. value = tmp & AT91_WDT_WDV;
  152. delta = (tmp & AT91_WDT_WDD) >> 16;
  153. if (delta < value)
  154. min_heartbeat = ticks_to_hz_roundup(value - delta);
  155. max_heartbeat = ticks_to_hz_rounddown(value);
  156. if (!max_heartbeat) {
  157. dev_err(dev,
  158. "heartbeat is too small for the system to handle it correctly\n");
  159. return -EINVAL;
  160. }
  161. /*
  162. * Try to reset the watchdog counter 4 or 2 times more often than
  163. * actually requested, to avoid spurious watchdog reset.
  164. * If this is not possible because of the min_heartbeat value, reset
  165. * it at the min_heartbeat period.
  166. */
  167. if ((max_heartbeat / 4) >= min_heartbeat)
  168. wdt->heartbeat = max_heartbeat / 4;
  169. else if ((max_heartbeat / 2) >= min_heartbeat)
  170. wdt->heartbeat = max_heartbeat / 2;
  171. else
  172. wdt->heartbeat = min_heartbeat;
  173. if (max_heartbeat < min_heartbeat + 4)
  174. dev_warn(dev,
  175. "min heartbeat and max heartbeat might be too close for the system to handle it correctly\n");
  176. if ((tmp & AT91_WDT_WDFIEN) && wdt->irq) {
  177. err = request_irq(wdt->irq, wdt_interrupt,
  178. IRQF_SHARED | IRQF_IRQPOLL,
  179. pdev->name, wdt);
  180. if (err)
  181. return err;
  182. }
  183. if ((tmp & wdt->mr_mask) != (wdt->mr & wdt->mr_mask))
  184. dev_warn(dev,
  185. "watchdog already configured differently (mr = %x expecting %x)\n",
  186. tmp & wdt->mr_mask, wdt->mr & wdt->mr_mask);
  187. setup_timer(&wdt->timer, at91_ping, (unsigned long)wdt);
  188. /*
  189. * Use min_heartbeat the first time to avoid spurious watchdog reset:
  190. * we don't know for how long the watchdog counter is running, and
  191. * - resetting it right now might trigger a watchdog fault reset
  192. * - waiting for heartbeat time might lead to a watchdog timeout
  193. * reset
  194. */
  195. mod_timer(&wdt->timer, jiffies + min_heartbeat);
  196. /* Try to set timeout from device tree first */
  197. if (watchdog_init_timeout(&wdt->wdd, 0, dev))
  198. watchdog_init_timeout(&wdt->wdd, heartbeat, dev);
  199. watchdog_set_nowayout(&wdt->wdd, wdt->nowayout);
  200. err = watchdog_register_device(&wdt->wdd);
  201. if (err)
  202. goto out_stop_timer;
  203. wdt->next_heartbeat = jiffies + wdt->wdd.timeout * HZ;
  204. return 0;
  205. out_stop_timer:
  206. del_timer(&wdt->timer);
  207. return err;
  208. }
  209. /* ......................................................................... */
  210. static const struct watchdog_info at91_wdt_info = {
  211. .identity = DRV_NAME,
  212. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  213. WDIOF_MAGICCLOSE,
  214. };
  215. static const struct watchdog_ops at91_wdt_ops = {
  216. .owner = THIS_MODULE,
  217. .start = at91_wdt_start,
  218. .stop = at91_wdt_stop,
  219. .set_timeout = at91_wdt_set_timeout,
  220. };
  221. #if defined(CONFIG_OF)
  222. static int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
  223. {
  224. u32 min = 0;
  225. u32 max = WDT_COUNTER_MAX_SECS;
  226. const char *tmp;
  227. /* Get the interrupts property */
  228. wdt->irq = irq_of_parse_and_map(np, 0);
  229. if (!wdt->irq)
  230. dev_warn(wdt->wdd.parent, "failed to get IRQ from DT\n");
  231. if (!of_property_read_u32_index(np, "atmel,max-heartbeat-sec", 0,
  232. &max)) {
  233. if (!max || max > WDT_COUNTER_MAX_SECS)
  234. max = WDT_COUNTER_MAX_SECS;
  235. if (!of_property_read_u32_index(np, "atmel,min-heartbeat-sec",
  236. 0, &min)) {
  237. if (min >= max)
  238. min = max - 1;
  239. }
  240. }
  241. min = secs_to_ticks(min);
  242. max = secs_to_ticks(max);
  243. wdt->mr_mask = 0x3FFFFFFF;
  244. wdt->mr = 0;
  245. if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
  246. !strcmp(tmp, "software")) {
  247. wdt->mr |= AT91_WDT_WDFIEN;
  248. wdt->mr_mask &= ~AT91_WDT_WDRPROC;
  249. } else {
  250. wdt->mr |= AT91_WDT_WDRSTEN;
  251. }
  252. if (!of_property_read_string(np, "atmel,reset-type", &tmp) &&
  253. !strcmp(tmp, "proc"))
  254. wdt->mr |= AT91_WDT_WDRPROC;
  255. if (of_property_read_bool(np, "atmel,disable")) {
  256. wdt->mr |= AT91_WDT_WDDIS;
  257. wdt->mr_mask &= AT91_WDT_WDDIS;
  258. }
  259. if (of_property_read_bool(np, "atmel,idle-halt"))
  260. wdt->mr |= AT91_WDT_WDIDLEHLT;
  261. if (of_property_read_bool(np, "atmel,dbg-halt"))
  262. wdt->mr |= AT91_WDT_WDDBGHLT;
  263. wdt->mr |= max | ((max - min) << 16);
  264. return 0;
  265. }
  266. #else
  267. static inline int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
  268. {
  269. return 0;
  270. }
  271. #endif
  272. static int __init at91wdt_probe(struct platform_device *pdev)
  273. {
  274. struct resource *r;
  275. int err;
  276. struct at91wdt *wdt;
  277. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  278. if (!wdt)
  279. return -ENOMEM;
  280. wdt->mr = (WDT_HW_TIMEOUT * 256) | AT91_WDT_WDRSTEN | AT91_WDT_WDD |
  281. AT91_WDT_WDDBGHLT | AT91_WDT_WDIDLEHLT;
  282. wdt->mr_mask = 0x3FFFFFFF;
  283. wdt->nowayout = nowayout;
  284. wdt->wdd.parent = &pdev->dev;
  285. wdt->wdd.info = &at91_wdt_info;
  286. wdt->wdd.ops = &at91_wdt_ops;
  287. wdt->wdd.timeout = WDT_HEARTBEAT;
  288. wdt->wdd.min_timeout = 1;
  289. wdt->wdd.max_timeout = 0xFFFF;
  290. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  291. wdt->base = devm_ioremap_resource(&pdev->dev, r);
  292. if (IS_ERR(wdt->base))
  293. return PTR_ERR(wdt->base);
  294. if (pdev->dev.of_node) {
  295. err = of_at91wdt_init(pdev->dev.of_node, wdt);
  296. if (err)
  297. return err;
  298. }
  299. err = at91_wdt_init(pdev, wdt);
  300. if (err)
  301. return err;
  302. platform_set_drvdata(pdev, wdt);
  303. pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
  304. wdt->wdd.timeout, wdt->nowayout);
  305. return 0;
  306. }
  307. static int __exit at91wdt_remove(struct platform_device *pdev)
  308. {
  309. struct at91wdt *wdt = platform_get_drvdata(pdev);
  310. watchdog_unregister_device(&wdt->wdd);
  311. pr_warn("I quit now, hardware will probably reboot!\n");
  312. del_timer(&wdt->timer);
  313. return 0;
  314. }
  315. #if defined(CONFIG_OF)
  316. static const struct of_device_id at91_wdt_dt_ids[] = {
  317. { .compatible = "atmel,at91sam9260-wdt" },
  318. { /* sentinel */ }
  319. };
  320. MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
  321. #endif
  322. static struct platform_driver at91wdt_driver = {
  323. .remove = __exit_p(at91wdt_remove),
  324. .driver = {
  325. .name = "at91_wdt",
  326. .owner = THIS_MODULE,
  327. .of_match_table = of_match_ptr(at91_wdt_dt_ids),
  328. },
  329. };
  330. module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
  331. MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
  332. MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
  333. MODULE_LICENSE("GPL");