mpc8xxx_wdt.c 6.9 KB

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