at91sam9_wdt.c 11 KB

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