mpc8xxx_wdt.c 8.1 KB

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