mpc8xxx_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/of_address.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/module.h>
  27. #include <linux/watchdog.h>
  28. #include <linux/io.h>
  29. #include <linux/uaccess.h>
  30. #include <sysdev/fsl_soc.h>
  31. struct mpc8xxx_wdt {
  32. __be32 res0;
  33. __be32 swcrr; /* System watchdog control register */
  34. #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
  35. #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
  36. #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
  37. #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
  38. __be32 swcnr; /* System watchdog count register */
  39. u8 res1[2];
  40. __be16 swsrr; /* System watchdog service register */
  41. u8 res2[0xF0];
  42. };
  43. struct mpc8xxx_wdt_type {
  44. int prescaler;
  45. bool hw_enabled;
  46. };
  47. struct mpc8xxx_wdt_ddata {
  48. struct mpc8xxx_wdt __iomem *base;
  49. struct watchdog_device wdd;
  50. struct timer_list timer;
  51. spinlock_t lock;
  52. };
  53. static u16 timeout = 0xffff;
  54. module_param(timeout, ushort, 0);
  55. MODULE_PARM_DESC(timeout,
  56. "Watchdog timeout in ticks. (0<timeout<65536, default=65535)");
  57. static bool reset = 1;
  58. module_param(reset, bool, 0);
  59. MODULE_PARM_DESC(reset,
  60. "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
  61. static bool nowayout = WATCHDOG_NOWAYOUT;
  62. module_param(nowayout, bool, 0);
  63. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  64. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  65. static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata)
  66. {
  67. /* Ping the WDT */
  68. spin_lock(&ddata->lock);
  69. out_be16(&ddata->base->swsrr, 0x556c);
  70. out_be16(&ddata->base->swsrr, 0xaa39);
  71. spin_unlock(&ddata->lock);
  72. }
  73. static void mpc8xxx_wdt_timer_ping(unsigned long arg)
  74. {
  75. struct mpc8xxx_wdt_ddata *ddata = (void *)arg;
  76. mpc8xxx_wdt_keepalive(ddata);
  77. /* We're pinging it twice faster than needed, just to be sure. */
  78. mod_timer(&ddata->timer, jiffies + HZ * ddata->wdd.timeout / 2);
  79. }
  80. static int mpc8xxx_wdt_start(struct watchdog_device *w)
  81. {
  82. struct mpc8xxx_wdt_ddata *ddata =
  83. container_of(w, struct mpc8xxx_wdt_ddata, wdd);
  84. u32 tmp = SWCRR_SWEN | SWCRR_SWPR;
  85. /* Good, fire up the show */
  86. if (reset)
  87. tmp |= SWCRR_SWRI;
  88. tmp |= timeout << 16;
  89. out_be32(&ddata->base->swcrr, tmp);
  90. del_timer_sync(&ddata->timer);
  91. return 0;
  92. }
  93. static int mpc8xxx_wdt_ping(struct watchdog_device *w)
  94. {
  95. struct mpc8xxx_wdt_ddata *ddata =
  96. container_of(w, struct mpc8xxx_wdt_ddata, wdd);
  97. mpc8xxx_wdt_keepalive(ddata);
  98. return 0;
  99. }
  100. static int mpc8xxx_wdt_stop(struct watchdog_device *w)
  101. {
  102. struct mpc8xxx_wdt_ddata *ddata =
  103. container_of(w, struct mpc8xxx_wdt_ddata, wdd);
  104. mod_timer(&ddata->timer, jiffies);
  105. return 0;
  106. }
  107. static struct watchdog_info mpc8xxx_wdt_info = {
  108. .options = WDIOF_KEEPALIVEPING,
  109. .firmware_version = 1,
  110. .identity = "MPC8xxx",
  111. };
  112. static struct watchdog_ops mpc8xxx_wdt_ops = {
  113. .owner = THIS_MODULE,
  114. .start = mpc8xxx_wdt_start,
  115. .ping = mpc8xxx_wdt_ping,
  116. .stop = mpc8xxx_wdt_stop,
  117. };
  118. static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
  119. {
  120. int ret;
  121. struct resource *res;
  122. const struct mpc8xxx_wdt_type *wdt_type;
  123. struct mpc8xxx_wdt_ddata *ddata;
  124. u32 freq = fsl_get_sys_freq();
  125. bool enabled;
  126. unsigned int timeout_sec;
  127. wdt_type = of_device_get_match_data(&ofdev->dev);
  128. if (!wdt_type)
  129. return -EINVAL;
  130. if (!freq || freq == -1)
  131. return -EINVAL;
  132. ddata = devm_kzalloc(&ofdev->dev, sizeof(*ddata), GFP_KERNEL);
  133. if (!ddata)
  134. return -ENOMEM;
  135. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  136. ddata->base = devm_ioremap_resource(&ofdev->dev, res);
  137. if (IS_ERR(ddata->base))
  138. return PTR_ERR(ddata->base);
  139. enabled = in_be32(&ddata->base->swcrr) & SWCRR_SWEN;
  140. if (!enabled && wdt_type->hw_enabled) {
  141. pr_info("could not be enabled in software\n");
  142. return -ENODEV;
  143. }
  144. spin_lock_init(&ddata->lock);
  145. setup_timer(&ddata->timer, mpc8xxx_wdt_timer_ping,
  146. (unsigned long)ddata);
  147. ddata->wdd.info = &mpc8xxx_wdt_info,
  148. ddata->wdd.ops = &mpc8xxx_wdt_ops,
  149. /* Calculate the timeout in seconds */
  150. timeout_sec = (timeout * wdt_type->prescaler) / freq;
  151. ddata->wdd.timeout = timeout_sec;
  152. watchdog_set_nowayout(&ddata->wdd, nowayout);
  153. ret = watchdog_register_device(&ddata->wdd);
  154. if (ret) {
  155. pr_err("cannot register watchdog device (err=%d)\n", ret);
  156. return ret;
  157. }
  158. pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d (%d seconds)\n",
  159. reset ? "reset" : "interrupt", timeout, timeout_sec);
  160. /*
  161. * If the watchdog was previously enabled or we're running on
  162. * MPC8xxx, we should ping the wdt from the kernel until the
  163. * userspace handles it.
  164. */
  165. if (enabled)
  166. mod_timer(&ddata->timer, jiffies);
  167. platform_set_drvdata(ofdev, ddata);
  168. return 0;
  169. }
  170. static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
  171. {
  172. struct mpc8xxx_wdt_ddata *ddata = platform_get_drvdata(ofdev);
  173. pr_crit("Watchdog removed, expect the %s soon!\n",
  174. reset ? "reset" : "machine check exception");
  175. del_timer_sync(&ddata->timer);
  176. watchdog_unregister_device(&ddata->wdd);
  177. return 0;
  178. }
  179. static const struct of_device_id mpc8xxx_wdt_match[] = {
  180. {
  181. .compatible = "mpc83xx_wdt",
  182. .data = &(struct mpc8xxx_wdt_type) {
  183. .prescaler = 0x10000,
  184. },
  185. },
  186. {
  187. .compatible = "fsl,mpc8610-wdt",
  188. .data = &(struct mpc8xxx_wdt_type) {
  189. .prescaler = 0x10000,
  190. .hw_enabled = true,
  191. },
  192. },
  193. {
  194. .compatible = "fsl,mpc823-wdt",
  195. .data = &(struct mpc8xxx_wdt_type) {
  196. .prescaler = 0x800,
  197. .hw_enabled = true,
  198. },
  199. },
  200. {},
  201. };
  202. MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
  203. static struct platform_driver mpc8xxx_wdt_driver = {
  204. .probe = mpc8xxx_wdt_probe,
  205. .remove = mpc8xxx_wdt_remove,
  206. .driver = {
  207. .name = "mpc8xxx_wdt",
  208. .of_match_table = mpc8xxx_wdt_match,
  209. },
  210. };
  211. static int __init mpc8xxx_wdt_init(void)
  212. {
  213. return platform_driver_register(&mpc8xxx_wdt_driver);
  214. }
  215. arch_initcall(mpc8xxx_wdt_init);
  216. static void __exit mpc8xxx_wdt_exit(void)
  217. {
  218. platform_driver_unregister(&mpc8xxx_wdt_driver);
  219. }
  220. module_exit(mpc8xxx_wdt_exit);
  221. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  222. MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
  223. "uProcessors");
  224. MODULE_LICENSE("GPL");