mpc8xxx_wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 unsigned int timeout_sec;
  68. static unsigned long wdt_is_open;
  69. static DEFINE_SPINLOCK(wdt_spinlock);
  70. static void mpc8xxx_wdt_keepalive(void)
  71. {
  72. /* Ping the WDT */
  73. spin_lock(&wdt_spinlock);
  74. out_be16(&wd_base->swsrr, 0x556c);
  75. out_be16(&wd_base->swsrr, 0xaa39);
  76. spin_unlock(&wdt_spinlock);
  77. }
  78. static void mpc8xxx_wdt_timer_ping(unsigned long arg);
  79. static DEFINE_TIMER(wdt_timer, mpc8xxx_wdt_timer_ping, 0, 0);
  80. static void mpc8xxx_wdt_timer_ping(unsigned long arg)
  81. {
  82. mpc8xxx_wdt_keepalive();
  83. /* We're pinging it twice faster than needed, just to be sure. */
  84. mod_timer(&wdt_timer, jiffies + HZ * timeout_sec / 2);
  85. }
  86. static void mpc8xxx_wdt_pr_warn(const char *msg)
  87. {
  88. pr_crit("%s, expect the %s soon!\n", msg,
  89. reset ? "reset" : "machine check exception");
  90. }
  91. static ssize_t mpc8xxx_wdt_write(struct file *file, const char __user *buf,
  92. size_t count, loff_t *ppos)
  93. {
  94. if (count)
  95. mpc8xxx_wdt_keepalive();
  96. return count;
  97. }
  98. static int mpc8xxx_wdt_open(struct inode *inode, struct file *file)
  99. {
  100. u32 tmp = SWCRR_SWEN;
  101. if (test_and_set_bit(0, &wdt_is_open))
  102. return -EBUSY;
  103. /* Once we start the watchdog we can't stop it */
  104. if (nowayout)
  105. __module_get(THIS_MODULE);
  106. /* Good, fire up the show */
  107. if (prescale)
  108. tmp |= SWCRR_SWPR;
  109. if (reset)
  110. tmp |= SWCRR_SWRI;
  111. tmp |= timeout << 16;
  112. out_be32(&wd_base->swcrr, tmp);
  113. del_timer_sync(&wdt_timer);
  114. return nonseekable_open(inode, file);
  115. }
  116. static int mpc8xxx_wdt_release(struct inode *inode, struct file *file)
  117. {
  118. if (!nowayout)
  119. mpc8xxx_wdt_timer_ping(0);
  120. else
  121. mpc8xxx_wdt_pr_warn("watchdog closed");
  122. clear_bit(0, &wdt_is_open);
  123. return 0;
  124. }
  125. static long mpc8xxx_wdt_ioctl(struct file *file, unsigned int cmd,
  126. unsigned long arg)
  127. {
  128. void __user *argp = (void __user *)arg;
  129. int __user *p = argp;
  130. static const struct watchdog_info ident = {
  131. .options = WDIOF_KEEPALIVEPING,
  132. .firmware_version = 1,
  133. .identity = "MPC8xxx",
  134. };
  135. switch (cmd) {
  136. case WDIOC_GETSUPPORT:
  137. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  138. case WDIOC_GETSTATUS:
  139. case WDIOC_GETBOOTSTATUS:
  140. return put_user(0, p);
  141. case WDIOC_KEEPALIVE:
  142. mpc8xxx_wdt_keepalive();
  143. return 0;
  144. case WDIOC_GETTIMEOUT:
  145. return put_user(timeout_sec, p);
  146. default:
  147. return -ENOTTY;
  148. }
  149. }
  150. static const struct file_operations mpc8xxx_wdt_fops = {
  151. .owner = THIS_MODULE,
  152. .llseek = no_llseek,
  153. .write = mpc8xxx_wdt_write,
  154. .unlocked_ioctl = mpc8xxx_wdt_ioctl,
  155. .open = mpc8xxx_wdt_open,
  156. .release = mpc8xxx_wdt_release,
  157. };
  158. static struct miscdevice mpc8xxx_wdt_miscdev = {
  159. .minor = WATCHDOG_MINOR,
  160. .name = "watchdog",
  161. .fops = &mpc8xxx_wdt_fops,
  162. };
  163. static const struct of_device_id mpc8xxx_wdt_match[];
  164. static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
  165. {
  166. int ret;
  167. const struct of_device_id *match;
  168. struct device_node *np = ofdev->dev.of_node;
  169. const struct mpc8xxx_wdt_type *wdt_type;
  170. u32 freq = fsl_get_sys_freq();
  171. bool enabled;
  172. match = of_match_device(mpc8xxx_wdt_match, &ofdev->dev);
  173. if (!match)
  174. return -EINVAL;
  175. wdt_type = match->data;
  176. if (!freq || freq == -1)
  177. return -EINVAL;
  178. wd_base = of_iomap(np, 0);
  179. if (!wd_base)
  180. return -ENOMEM;
  181. enabled = in_be32(&wd_base->swcrr) & SWCRR_SWEN;
  182. if (!enabled && wdt_type->hw_enabled) {
  183. pr_info("could not be enabled in software\n");
  184. ret = -ENOSYS;
  185. goto err_unmap;
  186. }
  187. /* Calculate the timeout in seconds */
  188. if (prescale)
  189. timeout_sec = (timeout * wdt_type->prescaler) / freq;
  190. else
  191. timeout_sec = timeout / freq;
  192. #ifdef MODULE
  193. ret = mpc8xxx_wdt_init_late();
  194. if (ret)
  195. goto err_unmap;
  196. #endif
  197. pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d (%d seconds)\n",
  198. reset ? "reset" : "interrupt", timeout, timeout_sec);
  199. /*
  200. * If the watchdog was previously enabled or we're running on
  201. * MPC8xxx, we should ping the wdt from the kernel until the
  202. * userspace handles it.
  203. */
  204. if (enabled)
  205. mpc8xxx_wdt_timer_ping(0);
  206. return 0;
  207. err_unmap:
  208. iounmap(wd_base);
  209. wd_base = NULL;
  210. return ret;
  211. }
  212. static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
  213. {
  214. mpc8xxx_wdt_pr_warn("watchdog removed");
  215. del_timer_sync(&wdt_timer);
  216. misc_deregister(&mpc8xxx_wdt_miscdev);
  217. iounmap(wd_base);
  218. return 0;
  219. }
  220. static const struct of_device_id mpc8xxx_wdt_match[] = {
  221. {
  222. .compatible = "mpc83xx_wdt",
  223. .data = &(struct mpc8xxx_wdt_type) {
  224. .prescaler = 0x10000,
  225. },
  226. },
  227. {
  228. .compatible = "fsl,mpc8610-wdt",
  229. .data = &(struct mpc8xxx_wdt_type) {
  230. .prescaler = 0x10000,
  231. .hw_enabled = true,
  232. },
  233. },
  234. {
  235. .compatible = "fsl,mpc823-wdt",
  236. .data = &(struct mpc8xxx_wdt_type) {
  237. .prescaler = 0x800,
  238. },
  239. },
  240. {},
  241. };
  242. MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
  243. static struct platform_driver mpc8xxx_wdt_driver = {
  244. .probe = mpc8xxx_wdt_probe,
  245. .remove = mpc8xxx_wdt_remove,
  246. .driver = {
  247. .name = "mpc8xxx_wdt",
  248. .owner = THIS_MODULE,
  249. .of_match_table = mpc8xxx_wdt_match,
  250. },
  251. };
  252. /*
  253. * We do wdt initialization in two steps: arch_initcall probes the wdt
  254. * very early to start pinging the watchdog (misc devices are not yet
  255. * available), and later module_init() just registers the misc device.
  256. */
  257. static int mpc8xxx_wdt_init_late(void)
  258. {
  259. int ret;
  260. if (!wd_base)
  261. return -ENODEV;
  262. ret = misc_register(&mpc8xxx_wdt_miscdev);
  263. if (ret) {
  264. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  265. WATCHDOG_MINOR, ret);
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. #ifndef MODULE
  271. module_init(mpc8xxx_wdt_init_late);
  272. #endif
  273. static int __init mpc8xxx_wdt_init(void)
  274. {
  275. return platform_driver_register(&mpc8xxx_wdt_driver);
  276. }
  277. arch_initcall(mpc8xxx_wdt_init);
  278. static void __exit mpc8xxx_wdt_exit(void)
  279. {
  280. platform_driver_unregister(&mpc8xxx_wdt_driver);
  281. }
  282. module_exit(mpc8xxx_wdt_exit);
  283. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  284. MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
  285. "uProcessors");
  286. MODULE_LICENSE("GPL");
  287. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);