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