mpc8xxx_wdt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface
  3. *
  4. * Authors: Dave Updegraff <dave@cray.org>
  5. * Kumar Gala <galak@kernel.crashing.org>
  6. * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
  7. * ..and from sc520_wdt
  8. * Copyright (c) 2008 MontaVista Software, Inc.
  9. * Anton Vorontsov <avorontsov@ru.mvista.com>
  10. *
  11. * Note: it appears that you can only actually ENABLE or DISABLE the thing
  12. * once after POR. Once enabled, you cannot disable, and vice versa.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/fs.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/module.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/io.h>
  30. #include <linux/uaccess.h>
  31. #include <sysdev/fsl_soc.h>
  32. struct mpc8xxx_wdt {
  33. __be32 res0;
  34. __be32 swcrr; /* System watchdog control register */
  35. #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
  36. #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
  37. #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
  38. #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
  39. __be32 swcnr; /* System watchdog count register */
  40. u8 res1[2];
  41. __be16 swsrr; /* System watchdog service register */
  42. u8 res2[0xF0];
  43. };
  44. struct mpc8xxx_wdt_type {
  45. int prescaler;
  46. bool hw_enabled;
  47. };
  48. static struct mpc8xxx_wdt __iomem *wd_base;
  49. static int mpc8xxx_wdt_init_late(void);
  50. static u16 timeout = 0xffff;
  51. module_param(timeout, ushort, 0);
  52. MODULE_PARM_DESC(timeout,
  53. "Watchdog timeout in ticks. (0<timeout<65536, default=65535)");
  54. static bool reset = 1;
  55. module_param(reset, bool, 0);
  56. MODULE_PARM_DESC(reset,
  57. "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
  58. static bool nowayout = WATCHDOG_NOWAYOUT;
  59. module_param(nowayout, bool, 0);
  60. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  61. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  62. /*
  63. * We always prescale, but if someone really doesn't want to they can set this
  64. * to 0
  65. */
  66. static int prescale = 1;
  67. static DEFINE_SPINLOCK(wdt_spinlock);
  68. static void mpc8xxx_wdt_keepalive(void)
  69. {
  70. /* Ping the WDT */
  71. spin_lock(&wdt_spinlock);
  72. out_be16(&wd_base->swsrr, 0x556c);
  73. out_be16(&wd_base->swsrr, 0xaa39);
  74. spin_unlock(&wdt_spinlock);
  75. }
  76. static struct watchdog_device mpc8xxx_wdt_dev;
  77. static void mpc8xxx_wdt_timer_ping(unsigned long arg);
  78. static DEFINE_TIMER(wdt_timer, mpc8xxx_wdt_timer_ping, 0,
  79. (unsigned long)&mpc8xxx_wdt_dev);
  80. static void mpc8xxx_wdt_timer_ping(unsigned long arg)
  81. {
  82. struct watchdog_device *w = (struct watchdog_device *)arg;
  83. mpc8xxx_wdt_keepalive();
  84. /* We're pinging it twice faster than needed, just to be sure. */
  85. mod_timer(&wdt_timer, jiffies + HZ * w->timeout / 2);
  86. }
  87. static int mpc8xxx_wdt_start(struct watchdog_device *w)
  88. {
  89. u32 tmp = SWCRR_SWEN;
  90. /* Good, fire up the show */
  91. if (prescale)
  92. tmp |= SWCRR_SWPR;
  93. if (reset)
  94. tmp |= SWCRR_SWRI;
  95. tmp |= timeout << 16;
  96. out_be32(&wd_base->swcrr, tmp);
  97. del_timer_sync(&wdt_timer);
  98. return 0;
  99. }
  100. static int mpc8xxx_wdt_ping(struct watchdog_device *w)
  101. {
  102. mpc8xxx_wdt_keepalive();
  103. return 0;
  104. }
  105. static int mpc8xxx_wdt_stop(struct watchdog_device *w)
  106. {
  107. mod_timer(&wdt_timer, jiffies);
  108. return 0;
  109. }
  110. static struct watchdog_info mpc8xxx_wdt_info = {
  111. .options = WDIOF_KEEPALIVEPING,
  112. .firmware_version = 1,
  113. .identity = "MPC8xxx",
  114. };
  115. static struct watchdog_ops mpc8xxx_wdt_ops = {
  116. .owner = THIS_MODULE,
  117. .start = mpc8xxx_wdt_start,
  118. .ping = mpc8xxx_wdt_ping,
  119. .stop = mpc8xxx_wdt_stop,
  120. };
  121. static struct watchdog_device mpc8xxx_wdt_dev = {
  122. .info = &mpc8xxx_wdt_info,
  123. .ops = &mpc8xxx_wdt_ops,
  124. };
  125. static const struct of_device_id mpc8xxx_wdt_match[];
  126. static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
  127. {
  128. int ret;
  129. const struct of_device_id *match;
  130. struct device_node *np = ofdev->dev.of_node;
  131. const struct mpc8xxx_wdt_type *wdt_type;
  132. u32 freq = fsl_get_sys_freq();
  133. bool enabled;
  134. unsigned int timeout_sec;
  135. match = of_match_device(mpc8xxx_wdt_match, &ofdev->dev);
  136. if (!match)
  137. return -EINVAL;
  138. wdt_type = match->data;
  139. if (!freq || freq == -1)
  140. return -EINVAL;
  141. wd_base = of_iomap(np, 0);
  142. if (!wd_base)
  143. return -ENOMEM;
  144. enabled = in_be32(&wd_base->swcrr) & SWCRR_SWEN;
  145. if (!enabled && wdt_type->hw_enabled) {
  146. pr_info("could not be enabled in software\n");
  147. ret = -ENOSYS;
  148. goto err_unmap;
  149. }
  150. /* Calculate the timeout in seconds */
  151. if (prescale)
  152. timeout_sec = (timeout * wdt_type->prescaler) / freq;
  153. else
  154. timeout_sec = timeout / freq;
  155. mpc8xxx_wdt_dev.timeout = timeout_sec;
  156. #ifdef MODULE
  157. ret = mpc8xxx_wdt_init_late();
  158. if (ret)
  159. goto err_unmap;
  160. #endif
  161. pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d (%d seconds)\n",
  162. reset ? "reset" : "interrupt", timeout, timeout_sec);
  163. /*
  164. * If the watchdog was previously enabled or we're running on
  165. * MPC8xxx, we should ping the wdt from the kernel until the
  166. * userspace handles it.
  167. */
  168. if (enabled)
  169. mod_timer(&wdt_timer, jiffies);
  170. return 0;
  171. err_unmap:
  172. iounmap(wd_base);
  173. wd_base = NULL;
  174. return ret;
  175. }
  176. static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
  177. {
  178. pr_crit("Watchdog removed, expect the %s soon!\n",
  179. reset ? "reset" : "machine check exception");
  180. del_timer_sync(&wdt_timer);
  181. watchdog_unregister_device(&mpc8xxx_wdt_dev);
  182. iounmap(wd_base);
  183. return 0;
  184. }
  185. static const struct of_device_id mpc8xxx_wdt_match[] = {
  186. {
  187. .compatible = "mpc83xx_wdt",
  188. .data = &(struct mpc8xxx_wdt_type) {
  189. .prescaler = 0x10000,
  190. },
  191. },
  192. {
  193. .compatible = "fsl,mpc8610-wdt",
  194. .data = &(struct mpc8xxx_wdt_type) {
  195. .prescaler = 0x10000,
  196. .hw_enabled = true,
  197. },
  198. },
  199. {
  200. .compatible = "fsl,mpc823-wdt",
  201. .data = &(struct mpc8xxx_wdt_type) {
  202. .prescaler = 0x800,
  203. .hw_enabled = true,
  204. },
  205. },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
  209. static struct platform_driver mpc8xxx_wdt_driver = {
  210. .probe = mpc8xxx_wdt_probe,
  211. .remove = mpc8xxx_wdt_remove,
  212. .driver = {
  213. .name = "mpc8xxx_wdt",
  214. .of_match_table = mpc8xxx_wdt_match,
  215. },
  216. };
  217. /*
  218. * We do wdt initialization in two steps: arch_initcall probes the wdt
  219. * very early to start pinging the watchdog (misc devices are not yet
  220. * available), and later module_init() just registers the misc device.
  221. */
  222. static int mpc8xxx_wdt_init_late(void)
  223. {
  224. int ret;
  225. if (!wd_base)
  226. return -ENODEV;
  227. watchdog_set_nowayout(&mpc8xxx_wdt_dev, nowayout);
  228. ret = watchdog_register_device(&mpc8xxx_wdt_dev);
  229. if (ret) {
  230. pr_err("cannot register watchdog device (err=%d)\n", ret);
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. #ifndef MODULE
  236. module_init(mpc8xxx_wdt_init_late);
  237. #endif
  238. static int __init mpc8xxx_wdt_init(void)
  239. {
  240. return platform_driver_register(&mpc8xxx_wdt_driver);
  241. }
  242. arch_initcall(mpc8xxx_wdt_init);
  243. static void __exit mpc8xxx_wdt_exit(void)
  244. {
  245. platform_driver_unregister(&mpc8xxx_wdt_driver);
  246. }
  247. module_exit(mpc8xxx_wdt_exit);
  248. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  249. MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
  250. "uProcessors");
  251. MODULE_LICENSE("GPL");