booke_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/smp.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/notifier.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/reg_booke.h>
  22. #include <asm/system.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. return 0;
  175. case WDIOC_GETTIMEOUT:
  176. return put_user(booke_wdt_period, p);
  177. default:
  178. return -ENOTTY;
  179. }
  180. return 0;
  181. }
  182. /* wdt_is_active stores wether or not the /dev/watchdog device is opened */
  183. static unsigned long wdt_is_active;
  184. static int booke_wdt_open(struct inode *inode, struct file *file)
  185. {
  186. /* /dev/watchdog can only be opened once */
  187. if (test_and_set_bit(0, &wdt_is_active))
  188. return -EBUSY;
  189. spin_lock(&booke_wdt_lock);
  190. if (booke_wdt_enabled == 0) {
  191. booke_wdt_enabled = 1;
  192. on_each_cpu(__booke_wdt_enable, NULL, 0);
  193. pr_debug("booke_wdt: watchdog enabled (timeout = %llu sec)\n",
  194. period_to_sec(booke_wdt_period));
  195. }
  196. spin_unlock(&booke_wdt_lock);
  197. return nonseekable_open(inode, file);
  198. }
  199. static int booke_wdt_release(struct inode *inode, struct file *file)
  200. {
  201. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  202. /* Normally, the watchdog is disabled when /dev/watchdog is closed, but
  203. * if CONFIG_WATCHDOG_NOWAYOUT is defined, then it means that the
  204. * watchdog should remain enabled. So we disable it only if
  205. * CONFIG_WATCHDOG_NOWAYOUT is not defined.
  206. */
  207. on_each_cpu(__booke_wdt_disable, NULL, 0);
  208. booke_wdt_enabled = 0;
  209. pr_debug("booke_wdt: watchdog disabled\n");
  210. #endif
  211. clear_bit(0, &wdt_is_active);
  212. return 0;
  213. }
  214. static const struct file_operations booke_wdt_fops = {
  215. .owner = THIS_MODULE,
  216. .llseek = no_llseek,
  217. .write = booke_wdt_write,
  218. .unlocked_ioctl = booke_wdt_ioctl,
  219. .open = booke_wdt_open,
  220. .release = booke_wdt_release,
  221. };
  222. static struct miscdevice booke_wdt_miscdev = {
  223. .minor = WATCHDOG_MINOR,
  224. .name = "watchdog",
  225. .fops = &booke_wdt_fops,
  226. };
  227. static void __exit booke_wdt_exit(void)
  228. {
  229. misc_deregister(&booke_wdt_miscdev);
  230. }
  231. static int __init booke_wdt_init(void)
  232. {
  233. int ret = 0;
  234. pr_info("booke_wdt: powerpc book-e watchdog driver loaded\n");
  235. ident.firmware_version = cur_cpu_spec->pvr_value;
  236. ret = misc_register(&booke_wdt_miscdev);
  237. if (ret) {
  238. pr_err("booke_wdt: cannot register device (minor=%u, ret=%i)\n",
  239. WATCHDOG_MINOR, ret);
  240. return ret;
  241. }
  242. spin_lock(&booke_wdt_lock);
  243. if (booke_wdt_enabled == 1) {
  244. pr_info("booke_wdt: watchdog enabled (timeout = %llu sec)\n",
  245. period_to_sec(booke_wdt_period));
  246. on_each_cpu(__booke_wdt_enable, NULL, 0);
  247. }
  248. spin_unlock(&booke_wdt_lock);
  249. return ret;
  250. }
  251. module_init(booke_wdt_init);
  252. module_exit(booke_wdt_exit);
  253. MODULE_DESCRIPTION("PowerPC Book-E watchdog driver");
  254. MODULE_LICENSE("GPL");