booke_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 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. #ifdef CONFIG_FSL_BOOKE
  33. #define WDT_PERIOD_DEFAULT 38 /* Ex. wdt_period=28 bus=333Mhz,reset=~40sec */
  34. #else
  35. #define WDT_PERIOD_DEFAULT 3 /* Refer to the PPC40x and PPC4xx manuals */
  36. #endif /* for timing information */
  37. u32 booke_wdt_enabled;
  38. u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
  39. #ifdef CONFIG_FSL_BOOKE
  40. #define WDTP(x) ((((x)&0x3)<<30)|(((x)&0x3c)<<15))
  41. #define WDTP_MASK (WDTP(0))
  42. #else
  43. #define WDTP(x) (TCR_WP(x))
  44. #define WDTP_MASK (TCR_WP_MASK)
  45. #endif
  46. static DEFINE_SPINLOCK(booke_wdt_lock);
  47. /* For the specified period, determine the number of seconds
  48. * corresponding to the reset time. There will be a watchdog
  49. * exception at approximately 3/5 of this time.
  50. *
  51. * The formula to calculate this is given by:
  52. * 2.5 * (2^(63-period+1)) / timebase_freq
  53. *
  54. * In order to simplify things, we assume that period is
  55. * at least 1. This will still result in a very long timeout.
  56. */
  57. static unsigned long long period_to_sec(unsigned int period)
  58. {
  59. unsigned long long tmp = 1ULL << (64 - period);
  60. unsigned long tmp2 = ppc_tb_freq;
  61. /* tmp may be a very large number and we don't want to overflow,
  62. * so divide the timebase freq instead of multiplying tmp
  63. */
  64. tmp2 = tmp2 / 5 * 2;
  65. do_div(tmp, tmp2);
  66. return tmp;
  67. }
  68. /*
  69. * This procedure will find the highest period which will give a timeout
  70. * greater than the one required. e.g. for a bus speed of 66666666 and
  71. * and a parameter of 2 secs, then this procedure will return a value of 38.
  72. */
  73. static unsigned int sec_to_period(unsigned int secs)
  74. {
  75. unsigned int period;
  76. for (period = 63; period > 0; period--) {
  77. if (period_to_sec(period) >= secs)
  78. return period;
  79. }
  80. return 0;
  81. }
  82. static void __booke_wdt_ping(void *data)
  83. {
  84. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  85. }
  86. static void booke_wdt_ping(void)
  87. {
  88. on_each_cpu(__booke_wdt_ping, NULL, 0);
  89. }
  90. static void __booke_wdt_enable(void *data)
  91. {
  92. u32 val;
  93. /* clear status before enabling watchdog */
  94. __booke_wdt_ping(NULL);
  95. val = mfspr(SPRN_TCR);
  96. val &= ~WDTP_MASK;
  97. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  98. mtspr(SPRN_TCR, val);
  99. }
  100. static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
  101. size_t count, loff_t *ppos)
  102. {
  103. booke_wdt_ping();
  104. return count;
  105. }
  106. static struct watchdog_info ident = {
  107. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  108. .identity = "PowerPC Book-E Watchdog",
  109. };
  110. static long booke_wdt_ioctl(struct file *file,
  111. unsigned int cmd, unsigned long arg)
  112. {
  113. u32 tmp = 0;
  114. u32 __user *p = (u32 __user *)arg;
  115. switch (cmd) {
  116. case WDIOC_GETSUPPORT:
  117. if (copy_to_user((void *)arg, &ident, sizeof(ident)))
  118. return -EFAULT;
  119. case WDIOC_GETSTATUS:
  120. return put_user(ident.options, p);
  121. case WDIOC_GETBOOTSTATUS:
  122. /* XXX: something is clearing TSR */
  123. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  124. /* returns 1 if last reset was caused by the WDT */
  125. return (tmp ? 1 : 0);
  126. case WDIOC_SETOPTIONS:
  127. if (get_user(tmp, p))
  128. return -EINVAL;
  129. if (tmp == WDIOS_ENABLECARD) {
  130. booke_wdt_ping();
  131. break;
  132. } else
  133. return -EINVAL;
  134. return 0;
  135. case WDIOC_KEEPALIVE:
  136. booke_wdt_ping();
  137. return 0;
  138. case WDIOC_SETTIMEOUT:
  139. if (get_user(tmp, p))
  140. return -EFAULT;
  141. #ifdef CONFIG_FSL_BOOKE
  142. /* period of 1 gives the largest possible timeout */
  143. if (tmp > period_to_sec(1))
  144. return -EINVAL;
  145. booke_wdt_period = sec_to_period(tmp);
  146. #else
  147. booke_wdt_period = tmp;
  148. #endif
  149. mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP_MASK) |
  150. WDTP(booke_wdt_period));
  151. return 0;
  152. case WDIOC_GETTIMEOUT:
  153. return put_user(booke_wdt_period, p);
  154. default:
  155. return -ENOTTY;
  156. }
  157. return 0;
  158. }
  159. static int booke_wdt_open(struct inode *inode, struct file *file)
  160. {
  161. spin_lock(&booke_wdt_lock);
  162. if (booke_wdt_enabled == 0) {
  163. booke_wdt_enabled = 1;
  164. on_each_cpu(__booke_wdt_enable, NULL, 0);
  165. printk(KERN_INFO
  166. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  167. booke_wdt_period);
  168. }
  169. spin_unlock(&booke_wdt_lock);
  170. return nonseekable_open(inode, file);
  171. }
  172. static const struct file_operations booke_wdt_fops = {
  173. .owner = THIS_MODULE,
  174. .llseek = no_llseek,
  175. .write = booke_wdt_write,
  176. .unlocked_ioctl = booke_wdt_ioctl,
  177. .open = booke_wdt_open,
  178. };
  179. static struct miscdevice booke_wdt_miscdev = {
  180. .minor = WATCHDOG_MINOR,
  181. .name = "watchdog",
  182. .fops = &booke_wdt_fops,
  183. };
  184. static void __exit booke_wdt_exit(void)
  185. {
  186. misc_deregister(&booke_wdt_miscdev);
  187. }
  188. static int __init booke_wdt_init(void)
  189. {
  190. int ret = 0;
  191. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  192. ident.firmware_version = cur_cpu_spec->pvr_value;
  193. ret = misc_register(&booke_wdt_miscdev);
  194. if (ret) {
  195. printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
  196. WATCHDOG_MINOR, ret);
  197. return ret;
  198. }
  199. spin_lock(&booke_wdt_lock);
  200. if (booke_wdt_enabled == 1) {
  201. printk(KERN_INFO
  202. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  203. booke_wdt_period);
  204. on_each_cpu(__booke_wdt_enable, NULL, 0);
  205. }
  206. spin_unlock(&booke_wdt_lock);
  207. return ret;
  208. }
  209. device_initcall(booke_wdt_init);