sa1100_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Watchdog driver for the SA11x0/PXA2xx
  3. *
  4. * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
  5. * Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Neither Oleg Drokin nor iXcelerator.com admit liability nor provide
  13. * warranty for any of this software. This material is provided
  14. * "AS-IS" and at no charge.
  15. *
  16. * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
  17. *
  18. * 27/11/2000 Initial release
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/clk.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/watchdog.h>
  29. #include <linux/init.h>
  30. #include <linux/io.h>
  31. #include <linux/bitops.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/timex.h>
  34. #ifdef CONFIG_ARCH_PXA
  35. #include <mach/regs-ost.h>
  36. #endif
  37. #include <mach/reset.h>
  38. #include <mach/hardware.h>
  39. static unsigned long oscr_freq;
  40. static unsigned long sa1100wdt_users;
  41. static unsigned int pre_margin;
  42. static int boot_status;
  43. /*
  44. * Allow only one person to hold it open
  45. */
  46. static int sa1100dog_open(struct inode *inode, struct file *file)
  47. {
  48. if (test_and_set_bit(1, &sa1100wdt_users))
  49. return -EBUSY;
  50. /* Activate SA1100 Watchdog timer */
  51. writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
  52. writel_relaxed(OSSR_M3, OSSR);
  53. writel_relaxed(OWER_WME, OWER);
  54. writel_relaxed(readl_relaxed(OIER) | OIER_E3, OIER);
  55. return nonseekable_open(inode, file);
  56. }
  57. /*
  58. * The watchdog cannot be disabled.
  59. *
  60. * Previous comments suggested that turning off the interrupt by
  61. * clearing OIER[E3] would prevent the watchdog timing out but this
  62. * does not appear to be true (at least on the PXA255).
  63. */
  64. static int sa1100dog_release(struct inode *inode, struct file *file)
  65. {
  66. pr_crit("Device closed - timer will not stop\n");
  67. clear_bit(1, &sa1100wdt_users);
  68. return 0;
  69. }
  70. static ssize_t sa1100dog_write(struct file *file, const char __user *data,
  71. size_t len, loff_t *ppos)
  72. {
  73. if (len)
  74. /* Refresh OSMR3 timer. */
  75. writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
  76. return len;
  77. }
  78. static const struct watchdog_info ident = {
  79. .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT
  80. | WDIOF_KEEPALIVEPING,
  81. .identity = "SA1100/PXA255 Watchdog",
  82. .firmware_version = 1,
  83. };
  84. static long sa1100dog_ioctl(struct file *file, unsigned int cmd,
  85. unsigned long arg)
  86. {
  87. int ret = -ENOTTY;
  88. int time;
  89. void __user *argp = (void __user *)arg;
  90. int __user *p = argp;
  91. switch (cmd) {
  92. case WDIOC_GETSUPPORT:
  93. ret = copy_to_user(argp, &ident,
  94. sizeof(ident)) ? -EFAULT : 0;
  95. break;
  96. case WDIOC_GETSTATUS:
  97. ret = put_user(0, p);
  98. break;
  99. case WDIOC_GETBOOTSTATUS:
  100. ret = put_user(boot_status, p);
  101. break;
  102. case WDIOC_KEEPALIVE:
  103. writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
  104. ret = 0;
  105. break;
  106. case WDIOC_SETTIMEOUT:
  107. ret = get_user(time, p);
  108. if (ret)
  109. break;
  110. if (time <= 0 || (oscr_freq * (long long)time >= 0xffffffff)) {
  111. ret = -EINVAL;
  112. break;
  113. }
  114. pre_margin = oscr_freq * time;
  115. writel_relaxed(readl_relaxed(OSCR) + pre_margin, OSMR3);
  116. /*fall through*/
  117. case WDIOC_GETTIMEOUT:
  118. ret = put_user(pre_margin / oscr_freq, p);
  119. break;
  120. }
  121. return ret;
  122. }
  123. static const struct file_operations sa1100dog_fops = {
  124. .owner = THIS_MODULE,
  125. .llseek = no_llseek,
  126. .write = sa1100dog_write,
  127. .unlocked_ioctl = sa1100dog_ioctl,
  128. .open = sa1100dog_open,
  129. .release = sa1100dog_release,
  130. };
  131. static struct miscdevice sa1100dog_miscdev = {
  132. .minor = WATCHDOG_MINOR,
  133. .name = "watchdog",
  134. .fops = &sa1100dog_fops,
  135. };
  136. static int margin __initdata = 60; /* (secs) Default is 1 minute */
  137. static struct clk *clk;
  138. static int __init sa1100dog_init(void)
  139. {
  140. int ret;
  141. clk = clk_get(NULL, "OSTIMER0");
  142. if (IS_ERR(clk)) {
  143. pr_err("SA1100/PXA2xx Watchdog Timer: clock not found: %d\n",
  144. (int) PTR_ERR(clk));
  145. return PTR_ERR(clk);
  146. }
  147. ret = clk_prepare_enable(clk);
  148. if (ret) {
  149. pr_err("SA1100/PXA2xx Watchdog Timer: clock failed to prepare+enable: %d\n",
  150. ret);
  151. goto err;
  152. }
  153. oscr_freq = clk_get_rate(clk);
  154. /*
  155. * Read the reset status, and save it for later. If
  156. * we suspend, RCSR will be cleared, and the watchdog
  157. * reset reason will be lost.
  158. */
  159. boot_status = (reset_status & RESET_STATUS_WATCHDOG) ?
  160. WDIOF_CARDRESET : 0;
  161. pre_margin = oscr_freq * margin;
  162. ret = misc_register(&sa1100dog_miscdev);
  163. if (ret == 0) {
  164. pr_info("SA1100/PXA2xx Watchdog Timer: timer margin %d sec\n",
  165. margin);
  166. return 0;
  167. }
  168. clk_disable_unprepare(clk);
  169. err:
  170. clk_put(clk);
  171. return ret;
  172. }
  173. static void __exit sa1100dog_exit(void)
  174. {
  175. misc_deregister(&sa1100dog_miscdev);
  176. clk_disable_unprepare(clk);
  177. clk_put(clk);
  178. }
  179. module_init(sa1100dog_init);
  180. module_exit(sa1100dog_exit);
  181. MODULE_AUTHOR("Oleg Drokin <green@crimea.edu>");
  182. MODULE_DESCRIPTION("SA1100/PXA2xx Watchdog");
  183. module_param(margin, int, 0);
  184. MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
  185. MODULE_LICENSE("GPL");