booke_wdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Watchdog timer for PowerPC Book-E systems
  3. *
  4. * Author: Matthew McClintock
  5. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  6. *
  7. * Copyright 2005, 2008, 2010-2011 Freescale Semiconductor Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/smp.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/notifier.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/reg_booke.h>
  23. #include <asm/time.h>
  24. #include <asm/div64.h>
  25. /* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
  26. * Also, the wdt_period sets the watchdog timer period timeout.
  27. * For E500 cpus the wdt_period sets which bit changing from 0->1 will
  28. * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
  29. * first time nothing will happen, the second time a watchdog exception will
  30. * occur, and the final time the board will reset.
  31. */
  32. u32 booke_wdt_enabled;
  33. u32 booke_wdt_period = CONFIG_BOOKE_WDT_DEFAULT_TIMEOUT;
  34. #ifdef CONFIG_FSL_BOOKE
  35. #define WDTP(x) ((((x)&0x3)<<30)|(((x)&0x3c)<<15))
  36. #define WDTP_MASK (WDTP(0x3f))
  37. #else
  38. #define WDTP(x) (TCR_WP(x))
  39. #define WDTP_MASK (TCR_WP_MASK)
  40. #endif
  41. static DEFINE_SPINLOCK(booke_wdt_lock);
  42. /* For the specified period, determine the number of seconds
  43. * corresponding to the reset time. There will be a watchdog
  44. * exception at approximately 3/5 of this time.
  45. *
  46. * The formula to calculate this is given by:
  47. * 2.5 * (2^(63-period+1)) / timebase_freq
  48. *
  49. * In order to simplify things, we assume that period is
  50. * at least 1. This will still result in a very long timeout.
  51. */
  52. static unsigned long long period_to_sec(unsigned int period)
  53. {
  54. unsigned long long tmp = 1ULL << (64 - period);
  55. unsigned long tmp2 = ppc_tb_freq;
  56. /* tmp may be a very large number and we don't want to overflow,
  57. * so divide the timebase freq instead of multiplying tmp
  58. */
  59. tmp2 = tmp2 / 5 * 2;
  60. do_div(tmp, tmp2);
  61. return tmp;
  62. }
  63. /*
  64. * This procedure will find the highest period which will give a timeout
  65. * greater than the one required. e.g. for a bus speed of 66666666 and
  66. * and a parameter of 2 secs, then this procedure will return a value of 38.
  67. */
  68. static unsigned int sec_to_period(unsigned int secs)
  69. {
  70. unsigned int period;
  71. for (period = 63; period > 0; period--) {
  72. if (period_to_sec(period) >= secs)
  73. return period;
  74. }
  75. return 0;
  76. }
  77. static void __booke_wdt_set(void *data)
  78. {
  79. u32 val;
  80. val = mfspr(SPRN_TCR);
  81. val &= ~WDTP_MASK;
  82. val |= WDTP(booke_wdt_period);
  83. mtspr(SPRN_TCR, val);
  84. }
  85. static void booke_wdt_set(void)
  86. {
  87. on_each_cpu(__booke_wdt_set, NULL, 0);
  88. }
  89. static void __booke_wdt_ping(void *data)
  90. {
  91. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  92. }
  93. static void booke_wdt_ping(void)
  94. {
  95. on_each_cpu(__booke_wdt_ping, NULL, 0);
  96. }
  97. static void __booke_wdt_enable(void *data)
  98. {
  99. u32 val;
  100. /* clear status before enabling watchdog */
  101. __booke_wdt_ping(NULL);
  102. val = mfspr(SPRN_TCR);
  103. val &= ~WDTP_MASK;
  104. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  105. mtspr(SPRN_TCR, val);
  106. }
  107. /**
  108. * booke_wdt_disable - disable the watchdog on the given CPU
  109. *
  110. * This function is called on each CPU. It disables the watchdog on that CPU.
  111. *
  112. * TCR[WRC] cannot be changed once it has been set to non-zero, but we can
  113. * effectively disable the watchdog by setting its period to the maximum value.
  114. */
  115. static void __booke_wdt_disable(void *data)
  116. {
  117. u32 val;
  118. val = mfspr(SPRN_TCR);
  119. val &= ~(TCR_WIE | WDTP_MASK);
  120. mtspr(SPRN_TCR, val);
  121. /* clear status to make sure nothing is pending */
  122. __booke_wdt_ping(NULL);
  123. }
  124. static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
  125. size_t count, loff_t *ppos)
  126. {
  127. booke_wdt_ping();
  128. return count;
  129. }
  130. static struct watchdog_info ident = {
  131. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  132. .identity = "PowerPC Book-E Watchdog",
  133. };
  134. static long booke_wdt_ioctl(struct file *file,
  135. unsigned int cmd, unsigned long arg)
  136. {
  137. u32 tmp = 0;
  138. u32 __user *p = (u32 __user *)arg;
  139. switch (cmd) {
  140. case WDIOC_GETSUPPORT:
  141. if (copy_to_user((void *)arg, &ident, sizeof(ident)))
  142. return -EFAULT;
  143. case WDIOC_GETSTATUS:
  144. return put_user(0, p);
  145. case WDIOC_GETBOOTSTATUS:
  146. /* XXX: something is clearing TSR */
  147. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  148. /* returns CARDRESET if last reset was caused by the WDT */
  149. return (tmp ? WDIOF_CARDRESET : 0);
  150. case WDIOC_SETOPTIONS:
  151. if (get_user(tmp, p))
  152. return -EINVAL;
  153. if (tmp == WDIOS_ENABLECARD) {
  154. booke_wdt_ping();
  155. break;
  156. } else
  157. return -EINVAL;
  158. return 0;
  159. case WDIOC_KEEPALIVE:
  160. booke_wdt_ping();
  161. return 0;
  162. case WDIOC_SETTIMEOUT:
  163. if (get_user(tmp, p))
  164. return -EFAULT;
  165. #ifdef CONFIG_FSL_BOOKE
  166. /* period of 1 gives the largest possible timeout */
  167. if (tmp > period_to_sec(1))
  168. return -EINVAL;
  169. booke_wdt_period = sec_to_period(tmp);
  170. #else
  171. booke_wdt_period = tmp;
  172. #endif
  173. booke_wdt_set();
  174. /* Fall */
  175. case WDIOC_GETTIMEOUT:
  176. #ifdef CONFIG_FSL_BOOKE
  177. return put_user(period_to_sec(booke_wdt_period), p);
  178. #else
  179. return put_user(booke_wdt_period, p);
  180. #endif
  181. default:
  182. return -ENOTTY;
  183. }
  184. return 0;
  185. }
  186. /* wdt_is_active stores wether or not the /dev/watchdog device is opened */
  187. static unsigned long wdt_is_active;
  188. static int booke_wdt_open(struct inode *inode, struct file *file)
  189. {
  190. /* /dev/watchdog can only be opened once */
  191. if (test_and_set_bit(0, &wdt_is_active))
  192. return -EBUSY;
  193. spin_lock(&booke_wdt_lock);
  194. if (booke_wdt_enabled == 0) {
  195. booke_wdt_enabled = 1;
  196. on_each_cpu(__booke_wdt_enable, NULL, 0);
  197. pr_debug("watchdog enabled (timeout = %llu sec)\n",
  198. period_to_sec(booke_wdt_period));
  199. }
  200. spin_unlock(&booke_wdt_lock);
  201. return nonseekable_open(inode, file);
  202. }
  203. static int booke_wdt_release(struct inode *inode, struct file *file)
  204. {
  205. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  206. /* Normally, the watchdog is disabled when /dev/watchdog is closed, but
  207. * if CONFIG_WATCHDOG_NOWAYOUT is defined, then it means that the
  208. * watchdog should remain enabled. So we disable it only if
  209. * CONFIG_WATCHDOG_NOWAYOUT is not defined.
  210. */
  211. on_each_cpu(__booke_wdt_disable, NULL, 0);
  212. booke_wdt_enabled = 0;
  213. pr_debug("watchdog disabled\n");
  214. #endif
  215. clear_bit(0, &wdt_is_active);
  216. return 0;
  217. }
  218. static const struct file_operations booke_wdt_fops = {
  219. .owner = THIS_MODULE,
  220. .llseek = no_llseek,
  221. .write = booke_wdt_write,
  222. .unlocked_ioctl = booke_wdt_ioctl,
  223. .open = booke_wdt_open,
  224. .release = booke_wdt_release,
  225. };
  226. static struct miscdevice booke_wdt_miscdev = {
  227. .minor = WATCHDOG_MINOR,
  228. .name = "watchdog",
  229. .fops = &booke_wdt_fops,
  230. };
  231. static void __exit booke_wdt_exit(void)
  232. {
  233. misc_deregister(&booke_wdt_miscdev);
  234. }
  235. static int __init booke_wdt_init(void)
  236. {
  237. int ret = 0;
  238. pr_info("powerpc book-e watchdog driver loaded\n");
  239. ident.firmware_version = cur_cpu_spec->pvr_value;
  240. ret = misc_register(&booke_wdt_miscdev);
  241. if (ret) {
  242. pr_err("cannot register device (minor=%u, ret=%i)\n",
  243. WATCHDOG_MINOR, ret);
  244. return ret;
  245. }
  246. spin_lock(&booke_wdt_lock);
  247. if (booke_wdt_enabled == 1) {
  248. pr_info("watchdog enabled (timeout = %llu sec)\n",
  249. period_to_sec(booke_wdt_period));
  250. on_each_cpu(__booke_wdt_enable, NULL, 0);
  251. }
  252. spin_unlock(&booke_wdt_lock);
  253. return ret;
  254. }
  255. module_init(booke_wdt_init);
  256. module_exit(booke_wdt_exit);
  257. MODULE_DESCRIPTION("PowerPC Book-E watchdog driver");
  258. MODULE_LICENSE("GPL");