mpc8xxx_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include <linux/fs.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/timer.h>
  23. #include <linux/miscdevice.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. struct mpc8xxx_wdt {
  31. __be32 res0;
  32. __be32 swcrr; /* System watchdog control register */
  33. #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
  34. #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
  35. #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
  36. #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
  37. __be32 swcnr; /* System watchdog count register */
  38. u8 res1[2];
  39. __be16 swsrr; /* System watchdog service register */
  40. u8 res2[0xF0];
  41. };
  42. struct mpc8xxx_wdt_type {
  43. int prescaler;
  44. bool hw_enabled;
  45. };
  46. static struct mpc8xxx_wdt __iomem *wd_base;
  47. static u16 timeout = 0xffff;
  48. module_param(timeout, ushort, 0);
  49. MODULE_PARM_DESC(timeout,
  50. "Watchdog timeout in ticks. (0<timeout<65536, default=65535");
  51. static int reset = 1;
  52. module_param(reset, bool, 0);
  53. MODULE_PARM_DESC(reset,
  54. "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
  55. static int nowayout = WATCHDOG_NOWAYOUT;
  56. module_param(nowayout, int, 0);
  57. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  58. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  59. /*
  60. * We always prescale, but if someone really doesn't want to they can set this
  61. * to 0
  62. */
  63. static int prescale = 1;
  64. static unsigned int timeout_sec;
  65. static unsigned long wdt_is_open;
  66. static DEFINE_SPINLOCK(wdt_spinlock);
  67. static void mpc8xxx_wdt_keepalive(void)
  68. {
  69. /* Ping the WDT */
  70. spin_lock(&wdt_spinlock);
  71. out_be16(&wd_base->swsrr, 0x556c);
  72. out_be16(&wd_base->swsrr, 0xaa39);
  73. spin_unlock(&wdt_spinlock);
  74. }
  75. static void mpc8xxx_wdt_timer_ping(unsigned long arg);
  76. static DEFINE_TIMER(wdt_timer, mpc8xxx_wdt_timer_ping, 0, 0);
  77. static void mpc8xxx_wdt_timer_ping(unsigned long arg)
  78. {
  79. mpc8xxx_wdt_keepalive();
  80. /* We're pinging it twice faster than needed, just to be sure. */
  81. mod_timer(&wdt_timer, jiffies + HZ * timeout_sec / 2);
  82. }
  83. static void mpc8xxx_wdt_pr_warn(const char *msg)
  84. {
  85. pr_crit("mpc8xxx_wdt: %s, expect the %s soon!\n", msg,
  86. reset ? "reset" : "machine check exception");
  87. }
  88. static ssize_t mpc8xxx_wdt_write(struct file *file, const char __user *buf,
  89. size_t count, loff_t *ppos)
  90. {
  91. if (count)
  92. mpc8xxx_wdt_keepalive();
  93. return count;
  94. }
  95. static int mpc8xxx_wdt_open(struct inode *inode, struct file *file)
  96. {
  97. u32 tmp = SWCRR_SWEN;
  98. if (test_and_set_bit(0, &wdt_is_open))
  99. return -EBUSY;
  100. /* Once we start the watchdog we can't stop it */
  101. if (nowayout)
  102. __module_get(THIS_MODULE);
  103. /* Good, fire up the show */
  104. if (prescale)
  105. tmp |= SWCRR_SWPR;
  106. if (reset)
  107. tmp |= SWCRR_SWRI;
  108. tmp |= timeout << 16;
  109. out_be32(&wd_base->swcrr, tmp);
  110. del_timer_sync(&wdt_timer);
  111. return nonseekable_open(inode, file);
  112. }
  113. static int mpc8xxx_wdt_release(struct inode *inode, struct file *file)
  114. {
  115. if (!nowayout)
  116. mpc8xxx_wdt_timer_ping(0);
  117. else
  118. mpc8xxx_wdt_pr_warn("watchdog closed");
  119. clear_bit(0, &wdt_is_open);
  120. return 0;
  121. }
  122. static long mpc8xxx_wdt_ioctl(struct file *file, unsigned int cmd,
  123. unsigned long arg)
  124. {
  125. void __user *argp = (void __user *)arg;
  126. int __user *p = argp;
  127. static struct watchdog_info ident = {
  128. .options = WDIOF_KEEPALIVEPING,
  129. .firmware_version = 1,
  130. .identity = "MPC8xxx",
  131. };
  132. switch (cmd) {
  133. case WDIOC_GETSUPPORT:
  134. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  135. case WDIOC_GETSTATUS:
  136. case WDIOC_GETBOOTSTATUS:
  137. return put_user(0, p);
  138. case WDIOC_KEEPALIVE:
  139. mpc8xxx_wdt_keepalive();
  140. return 0;
  141. case WDIOC_GETTIMEOUT:
  142. return put_user(timeout_sec, p);
  143. default:
  144. return -ENOTTY;
  145. }
  146. }
  147. static const struct file_operations mpc8xxx_wdt_fops = {
  148. .owner = THIS_MODULE,
  149. .llseek = no_llseek,
  150. .write = mpc8xxx_wdt_write,
  151. .unlocked_ioctl = mpc8xxx_wdt_ioctl,
  152. .open = mpc8xxx_wdt_open,
  153. .release = mpc8xxx_wdt_release,
  154. };
  155. static struct miscdevice mpc8xxx_wdt_miscdev = {
  156. .minor = WATCHDOG_MINOR,
  157. .name = "watchdog",
  158. .fops = &mpc8xxx_wdt_fops,
  159. };
  160. static int __devinit mpc8xxx_wdt_probe(struct of_device *ofdev,
  161. const struct of_device_id *match)
  162. {
  163. int ret;
  164. struct device_node *np = ofdev->node;
  165. struct mpc8xxx_wdt_type *wdt_type = match->data;
  166. u32 freq = fsl_get_sys_freq();
  167. bool enabled;
  168. if (!freq || freq == -1)
  169. return -EINVAL;
  170. wd_base = of_iomap(np, 0);
  171. if (!wd_base)
  172. return -ENOMEM;
  173. enabled = in_be32(&wd_base->swcrr) & SWCRR_SWEN;
  174. if (!enabled && wdt_type->hw_enabled) {
  175. pr_info("mpc8xxx_wdt: could not be enabled in software\n");
  176. ret = -ENOSYS;
  177. goto err_unmap;
  178. }
  179. /* Calculate the timeout in seconds */
  180. if (prescale)
  181. timeout_sec = (timeout * wdt_type->prescaler) / freq;
  182. else
  183. timeout_sec = timeout / freq;
  184. pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d "
  185. "(%d seconds)\n", reset ? "reset" : "interrupt", timeout,
  186. timeout_sec);
  187. /*
  188. * If the watchdog was previously enabled or we're running on
  189. * MPC8xxx, we should ping the wdt from the kernel until the
  190. * userspace handles it.
  191. */
  192. if (enabled)
  193. mpc8xxx_wdt_timer_ping(0);
  194. return 0;
  195. err_unmap:
  196. iounmap(wd_base);
  197. wd_base = NULL;
  198. return ret;
  199. }
  200. static int __devexit mpc8xxx_wdt_remove(struct of_device *ofdev)
  201. {
  202. mpc8xxx_wdt_pr_warn("watchdog removed");
  203. del_timer_sync(&wdt_timer);
  204. misc_deregister(&mpc8xxx_wdt_miscdev);
  205. iounmap(wd_base);
  206. return 0;
  207. }
  208. static const struct of_device_id mpc8xxx_wdt_match[] = {
  209. {
  210. .compatible = "mpc83xx_wdt",
  211. .data = &(struct mpc8xxx_wdt_type) {
  212. .prescaler = 0x10000,
  213. },
  214. },
  215. {
  216. .compatible = "fsl,mpc8610-wdt",
  217. .data = &(struct mpc8xxx_wdt_type) {
  218. .prescaler = 0x10000,
  219. .hw_enabled = true,
  220. },
  221. },
  222. {
  223. .compatible = "fsl,mpc823-wdt",
  224. .data = &(struct mpc8xxx_wdt_type) {
  225. .prescaler = 0x800,
  226. },
  227. },
  228. {},
  229. };
  230. MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
  231. static struct of_platform_driver mpc8xxx_wdt_driver = {
  232. .match_table = mpc8xxx_wdt_match,
  233. .probe = mpc8xxx_wdt_probe,
  234. .remove = __devexit_p(mpc8xxx_wdt_remove),
  235. .driver = {
  236. .name = "mpc8xxx_wdt",
  237. .owner = THIS_MODULE,
  238. },
  239. };
  240. /*
  241. * We do wdt initialization in two steps: arch_initcall probes the wdt
  242. * very early to start pinging the watchdog (misc devices are not yet
  243. * available), and later module_init() just registers the misc device.
  244. */
  245. static int __init mpc8xxx_wdt_init_late(void)
  246. {
  247. int ret;
  248. if (!wd_base)
  249. return -ENODEV;
  250. ret = misc_register(&mpc8xxx_wdt_miscdev);
  251. if (ret) {
  252. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  253. WATCHDOG_MINOR, ret);
  254. return ret;
  255. }
  256. return 0;
  257. }
  258. module_init(mpc8xxx_wdt_init_late);
  259. static int __init mpc8xxx_wdt_init(void)
  260. {
  261. return of_register_platform_driver(&mpc8xxx_wdt_driver);
  262. }
  263. arch_initcall(mpc8xxx_wdt_init);
  264. static void __exit mpc8xxx_wdt_exit(void)
  265. {
  266. of_unregister_platform_driver(&mpc8xxx_wdt_driver);
  267. }
  268. module_exit(mpc8xxx_wdt_exit);
  269. MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
  270. MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
  271. "uProcessors");
  272. MODULE_LICENSE("GPL");
  273. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);