booke_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
  24. * Also, the wdt_period sets the watchdog timer period timeout.
  25. * For E500 cpus the wdt_period sets which bit changing from 0->1 will
  26. * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
  27. * first time nothing will happen, the second time a watchdog exception will
  28. * occur, and the final time the board will reset.
  29. */
  30. #ifdef CONFIG_FSL_BOOKE
  31. #define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz,reset=~40sec */
  32. #else
  33. #define WDT_PERIOD_DEFAULT 3 /* Refer to the PPC40x and PPC4xx manuals */
  34. #endif /* for timing information */
  35. u32 booke_wdt_enabled;
  36. u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
  37. #ifdef CONFIG_FSL_BOOKE
  38. #define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
  39. #define WDTP_MASK (WDTP(0))
  40. #else
  41. #define WDTP(x) (TCR_WP(x))
  42. #define WDTP_MASK (TCR_WP_MASK)
  43. #endif
  44. static DEFINE_SPINLOCK(booke_wdt_lock);
  45. static void __booke_wdt_ping(void *data)
  46. {
  47. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  48. }
  49. static void booke_wdt_ping(void)
  50. {
  51. on_each_cpu(__booke_wdt_ping, NULL, 0);
  52. }
  53. static void __booke_wdt_enable(void *data)
  54. {
  55. u32 val;
  56. /* clear status before enabling watchdog */
  57. __booke_wdt_ping(NULL);
  58. val = mfspr(SPRN_TCR);
  59. val &= ~WDTP_MASK;
  60. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  61. mtspr(SPRN_TCR, val);
  62. }
  63. static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
  64. size_t count, loff_t *ppos)
  65. {
  66. booke_wdt_ping();
  67. return count;
  68. }
  69. static struct watchdog_info ident = {
  70. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  71. .identity = "PowerPC Book-E Watchdog",
  72. };
  73. static long booke_wdt_ioctl(struct file *file,
  74. unsigned int cmd, unsigned long arg)
  75. {
  76. u32 tmp = 0;
  77. u32 __user *p = (u32 __user *)arg;
  78. switch (cmd) {
  79. case WDIOC_GETSUPPORT:
  80. if (copy_to_user(arg, &ident, sizeof(struct watchdog_info)))
  81. return -EFAULT;
  82. case WDIOC_GETSTATUS:
  83. return put_user(ident.options, p);
  84. case WDIOC_GETBOOTSTATUS:
  85. /* XXX: something is clearing TSR */
  86. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  87. /* returns 1 if last reset was caused by the WDT */
  88. return (tmp ? 1 : 0);
  89. case WDIOC_SETOPTIONS:
  90. if (get_user(tmp, p))
  91. return -EINVAL;
  92. if (tmp == WDIOS_ENABLECARD) {
  93. booke_wdt_ping();
  94. break;
  95. } else
  96. return -EINVAL;
  97. return 0;
  98. case WDIOC_KEEPALIVE:
  99. booke_wdt_ping();
  100. return 0;
  101. case WDIOC_SETTIMEOUT:
  102. if (get_user(booke_wdt_period, p))
  103. return -EFAULT;
  104. mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP_MASK) |
  105. WDTP(booke_wdt_period));
  106. return 0;
  107. case WDIOC_GETTIMEOUT:
  108. return put_user(booke_wdt_period, p);
  109. default:
  110. return -ENOTTY;
  111. }
  112. return 0;
  113. }
  114. static int booke_wdt_open(struct inode *inode, struct file *file)
  115. {
  116. spin_lock(&booke_wdt_lock);
  117. if (booke_wdt_enabled == 0) {
  118. booke_wdt_enabled = 1;
  119. on_each_cpu(__booke_wdt_enable, NULL, 0);
  120. printk(KERN_INFO
  121. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  122. booke_wdt_period);
  123. }
  124. spin_unlock(&booke_wdt_lock);
  125. return nonseekable_open(inode, file);
  126. }
  127. static const struct file_operations booke_wdt_fops = {
  128. .owner = THIS_MODULE,
  129. .llseek = no_llseek,
  130. .write = booke_wdt_write,
  131. .unlocked_ioctl = booke_wdt_ioctl,
  132. .open = booke_wdt_open,
  133. };
  134. static struct miscdevice booke_wdt_miscdev = {
  135. .minor = WATCHDOG_MINOR,
  136. .name = "watchdog",
  137. .fops = &booke_wdt_fops,
  138. };
  139. static void __exit booke_wdt_exit(void)
  140. {
  141. misc_deregister(&booke_wdt_miscdev);
  142. }
  143. static int __init booke_wdt_init(void)
  144. {
  145. int ret = 0;
  146. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  147. ident.firmware_version = cur_cpu_spec->pvr_value;
  148. ret = misc_register(&booke_wdt_miscdev);
  149. if (ret) {
  150. printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
  151. WATCHDOG_MINOR, ret);
  152. return ret;
  153. }
  154. spin_lock(&booke_wdt_lock);
  155. if (booke_wdt_enabled == 1) {
  156. printk(KERN_INFO
  157. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  158. booke_wdt_period);
  159. on_each_cpu(__booke_wdt_enable, NULL, 0);
  160. }
  161. spin_unlock(&booke_wdt_lock);
  162. return ret;
  163. }
  164. device_initcall(booke_wdt_init);