mpc8xxx_wdt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface
  4. *
  5. * Authors: Dave Updegraff <dave@cray.org>
  6. * Kumar Gala <galak@kernel.crashing.org>
  7. * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
  8. * ..and from sc520_wdt
  9. * Copyright (c) 2008 MontaVista Software, Inc.
  10. * Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * Note: it appears that you can only actually ENABLE or DISABLE the thing
  13. * once after POR. Once enabled, you cannot disable, and vice versa.
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/module.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/io.h>
  23. #include <linux/uaccess.h>
  24. #include <sysdev/fsl_soc.h>
  25. #define WATCHDOG_TIMEOUT 10
  26. struct mpc8xxx_wdt {
  27. __be32 res0;
  28. __be32 swcrr; /* System watchdog control register */
  29. #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
  30. #define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */
  31. #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
  32. #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
  33. #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
  34. __be32 swcnr; /* System watchdog count register */
  35. u8 res1[2];
  36. __be16 swsrr; /* System watchdog service register */
  37. u8 res2[0xF0];
  38. };
  39. struct mpc8xxx_wdt_type {
  40. int prescaler;
  41. bool hw_enabled;
  42. u32 rsr_mask;
  43. };
  44. struct mpc8xxx_wdt_ddata {
  45. struct mpc8xxx_wdt __iomem *base;
  46. struct watchdog_device wdd;
  47. spinlock_t lock;
  48. u16 swtc;
  49. };
  50. static u16 timeout;
  51. module_param(timeout, ushort, 0);
  52. MODULE_PARM_DESC(timeout,
  53. "Watchdog timeout in seconds. (1<timeout<65535, default="
  54. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  55. static bool reset = 1;
  56. module_param(reset, bool, 0);
  57. MODULE_PARM_DESC(reset,
  58. "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
  59. static bool nowayout = WATCHDOG_NOWAYOUT;
  60. module_param(nowayout, bool, 0);
  61. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  62. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  63. static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata)
  64. {
  65. /* Ping the WDT */
  66. spin_lock(&ddata->lock);
  67. out_be16(&ddata->base->swsrr, 0x556c);
  68. out_be16(&ddata->base->swsrr, 0xaa39);
  69. spin_unlock(&ddata->lock);
  70. }
  71. static int mpc8xxx_wdt_start(struct watchdog_device *w)
  72. {
  73. struct mpc8xxx_wdt_ddata *ddata =
  74. container_of(w, struct mpc8xxx_wdt_ddata, wdd);
  75. u32 tmp = in_be32(&ddata->base->swcrr);
  76. /* Good, fire up the show */
  77. tmp &= ~(SWCRR_SWTC | SWCRR_SWF | SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR);
  78. tmp |= SWCRR_SWEN | SWCRR_SWPR | (ddata->swtc << 16);
  79. if (reset)
  80. tmp |= SWCRR_SWRI;
  81. out_be32(&ddata->base->swcrr, tmp);
  82. tmp = in_be32(&ddata->base->swcrr);
  83. if (!(tmp & SWCRR_SWEN))
  84. return -EOPNOTSUPP;
  85. ddata->swtc = tmp >> 16;
  86. set_bit(WDOG_HW_RUNNING, &ddata->wdd.status);
  87. mpc8xxx_wdt_keepalive(ddata);
  88. return 0;
  89. }
  90. static int mpc8xxx_wdt_ping(struct watchdog_device *w)
  91. {
  92. struct mpc8xxx_wdt_ddata *ddata =
  93. container_of(w, struct mpc8xxx_wdt_ddata, wdd);
  94. mpc8xxx_wdt_keepalive(ddata);
  95. return 0;
  96. }
  97. static struct watchdog_info mpc8xxx_wdt_info = {
  98. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
  99. .firmware_version = 1,
  100. .identity = "MPC8xxx",
  101. };
  102. static const struct watchdog_ops mpc8xxx_wdt_ops = {
  103. .owner = THIS_MODULE,
  104. .start = mpc8xxx_wdt_start,
  105. .ping = mpc8xxx_wdt_ping,
  106. };
  107. static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
  108. {
  109. int ret;
  110. struct resource *res;
  111. const struct mpc8xxx_wdt_type *wdt_type;
  112. struct mpc8xxx_wdt_ddata *ddata;
  113. u32 freq = fsl_get_sys_freq();
  114. bool enabled;
  115. struct device *dev = &ofdev->dev;
  116. wdt_type = of_device_get_match_data(dev);
  117. if (!wdt_type)
  118. return -EINVAL;
  119. if (!freq || freq == -1)
  120. return -EINVAL;
  121. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  122. if (!ddata)
  123. return -ENOMEM;
  124. ddata->base = devm_platform_ioremap_resource(ofdev, 0);
  125. if (IS_ERR(ddata->base))
  126. return PTR_ERR(ddata->base);
  127. enabled = in_be32(&ddata->base->swcrr) & SWCRR_SWEN;
  128. if (!enabled && wdt_type->hw_enabled) {
  129. dev_info(dev, "could not be enabled in software\n");
  130. return -ENODEV;
  131. }
  132. res = platform_get_resource(ofdev, IORESOURCE_MEM, 1);
  133. if (res) {
  134. bool status;
  135. u32 __iomem *rsr = ioremap(res->start, resource_size(res));
  136. if (!rsr)
  137. return -ENOMEM;
  138. status = in_be32(rsr) & wdt_type->rsr_mask;
  139. ddata->wdd.bootstatus = status ? WDIOF_CARDRESET : 0;
  140. /* clear reset status bits related to watchdog timer */
  141. out_be32(rsr, wdt_type->rsr_mask);
  142. iounmap(rsr);
  143. dev_info(dev, "Last boot was %scaused by watchdog\n",
  144. status ? "" : "not ");
  145. }
  146. spin_lock_init(&ddata->lock);
  147. ddata->wdd.info = &mpc8xxx_wdt_info;
  148. ddata->wdd.ops = &mpc8xxx_wdt_ops;
  149. ddata->wdd.timeout = WATCHDOG_TIMEOUT;
  150. watchdog_init_timeout(&ddata->wdd, timeout, dev);
  151. watchdog_set_nowayout(&ddata->wdd, nowayout);
  152. ddata->swtc = min(ddata->wdd.timeout * freq / wdt_type->prescaler,
  153. 0xffffU);
  154. /*
  155. * If the watchdog was previously enabled or we're running on
  156. * MPC8xxx, we should ping the wdt from the kernel until the
  157. * userspace handles it.
  158. */
  159. if (enabled)
  160. mpc8xxx_wdt_start(&ddata->wdd);
  161. ddata->wdd.max_hw_heartbeat_ms = (ddata->swtc * wdt_type->prescaler) /
  162. (freq / 1000);
  163. ddata->wdd.min_timeout = ddata->wdd.max_hw_heartbeat_ms / 1000;
  164. if (ddata->wdd.timeout < ddata->wdd.min_timeout)
  165. ddata->wdd.timeout = ddata->wdd.min_timeout;
  166. ret = devm_watchdog_register_device(dev, &ddata->wdd);
  167. if (ret)
  168. return ret;
  169. dev_info(dev,
  170. "WDT driver for MPC8xxx initialized. mode:%s timeout=%d sec\n",
  171. reset ? "reset" : "interrupt", ddata->wdd.timeout);
  172. platform_set_drvdata(ofdev, ddata);
  173. return 0;
  174. }
  175. static const struct of_device_id mpc8xxx_wdt_match[] = {
  176. {
  177. .compatible = "mpc83xx_wdt",
  178. .data = &(struct mpc8xxx_wdt_type) {
  179. .prescaler = 0x10000,
  180. .rsr_mask = BIT(3), /* RSR Bit SWRS */
  181. },
  182. },
  183. {
  184. .compatible = "fsl,mpc8610-wdt",
  185. .data = &(struct mpc8xxx_wdt_type) {
  186. .prescaler = 0x10000,
  187. .hw_enabled = true,
  188. .rsr_mask = BIT(20), /* RSTRSCR Bit WDT_RR */
  189. },
  190. },
  191. {
  192. .compatible = "fsl,mpc823-wdt",
  193. .data = &(struct mpc8xxx_wdt_type) {
  194. .prescaler = 0x800,
  195. .hw_enabled = true,
  196. .rsr_mask = BIT(28), /* RSR Bit SWRS */
  197. },
  198. },
  199. {},
  200. };
  201. MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
  202. static struct platform_driver mpc8xxx_wdt_driver = {
  203. .probe = mpc8xxx_wdt_probe,
  204. .driver = {
  205. .name = "mpc8xxx_wdt",
  206. .of_match_table = mpc8xxx_wdt_match,
  207. },
  208. };
  209. static int __init mpc8xxx_wdt_init(void)
  210. {
  211. return platform_driver_register(&mpc8xxx_wdt_driver);
  212. }
  213. arch_initcall(mpc8xxx_wdt_init);
  214. static void __exit mpc8xxx_wdt_exit(void)
  215. {
  216. platform_driver_unregister(&mpc8xxx_wdt_driver);
  217. }
  218. module_exit(mpc8xxx_wdt_exit);
  219. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  220. MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
  221. "uProcessors");
  222. MODULE_LICENSE("GPL");