booke_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #else
  40. #define WDTP(x) (TCR_WP(x))
  41. #endif
  42. static DEFINE_SPINLOCK(booke_wdt_lock);
  43. static void __booke_wdt_ping(void *data)
  44. {
  45. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  46. }
  47. static void booke_wdt_ping(void)
  48. {
  49. on_each_cpu(__booke_wdt_ping, NULL, 0);
  50. }
  51. static void __booke_wdt_enable(void *data)
  52. {
  53. u32 val;
  54. /* clear status before enabling watchdog */
  55. __booke_wdt_ping(NULL);
  56. val = mfspr(SPRN_TCR);
  57. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  58. mtspr(SPRN_TCR, val);
  59. }
  60. static ssize_t booke_wdt_write(struct file *file, const char __user *buf,
  61. size_t count, loff_t *ppos)
  62. {
  63. booke_wdt_ping();
  64. return count;
  65. }
  66. static struct watchdog_info ident = {
  67. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  68. .identity = "PowerPC Book-E Watchdog",
  69. };
  70. static long booke_wdt_ioctl(struct file *file,
  71. unsigned int cmd, unsigned long arg)
  72. {
  73. u32 tmp = 0;
  74. u32 __user *p = (u32 __user *)arg;
  75. switch (cmd) {
  76. case WDIOC_GETSUPPORT:
  77. if (copy_to_user(arg, &ident, sizeof(struct watchdog_info)))
  78. return -EFAULT;
  79. case WDIOC_GETSTATUS:
  80. return put_user(ident.options, p);
  81. case WDIOC_GETBOOTSTATUS:
  82. /* XXX: something is clearing TSR */
  83. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  84. /* returns 1 if last reset was caused by the WDT */
  85. return (tmp ? 1 : 0);
  86. case WDIOC_SETOPTIONS:
  87. if (get_user(tmp, p))
  88. return -EINVAL;
  89. if (tmp == WDIOS_ENABLECARD) {
  90. booke_wdt_ping();
  91. break;
  92. } else
  93. return -EINVAL;
  94. return 0;
  95. case WDIOC_KEEPALIVE:
  96. booke_wdt_ping();
  97. return 0;
  98. case WDIOC_SETTIMEOUT:
  99. if (get_user(booke_wdt_period, p))
  100. return -EFAULT;
  101. mtspr(SPRN_TCR, (mfspr(SPRN_TCR) & ~WDTP(0)) |
  102. WDTP(booke_wdt_period));
  103. return 0;
  104. case WDIOC_GETTIMEOUT:
  105. return put_user(booke_wdt_period, p);
  106. default:
  107. return -ENOTTY;
  108. }
  109. return 0;
  110. }
  111. static int booke_wdt_open(struct inode *inode, struct file *file)
  112. {
  113. spin_lock(&booke_wdt_lock);
  114. if (booke_wdt_enabled == 0) {
  115. booke_wdt_enabled = 1;
  116. on_each_cpu(__booke_wdt_enable, NULL, 0);
  117. printk(KERN_INFO
  118. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  119. booke_wdt_period);
  120. }
  121. spin_unlock(&booke_wdt_lock);
  122. return nonseekable_open(inode, file);
  123. }
  124. static const struct file_operations booke_wdt_fops = {
  125. .owner = THIS_MODULE,
  126. .llseek = no_llseek,
  127. .write = booke_wdt_write,
  128. .unlocked_ioctl = booke_wdt_ioctl,
  129. .open = booke_wdt_open,
  130. };
  131. static struct miscdevice booke_wdt_miscdev = {
  132. .minor = WATCHDOG_MINOR,
  133. .name = "watchdog",
  134. .fops = &booke_wdt_fops,
  135. };
  136. static void __exit booke_wdt_exit(void)
  137. {
  138. misc_deregister(&booke_wdt_miscdev);
  139. }
  140. static int __init booke_wdt_init(void)
  141. {
  142. int ret = 0;
  143. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  144. ident.firmware_version = cur_cpu_spec->pvr_value;
  145. ret = misc_register(&booke_wdt_miscdev);
  146. if (ret) {
  147. printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
  148. WATCHDOG_MINOR, ret);
  149. return ret;
  150. }
  151. spin_lock(&booke_wdt_lock);
  152. if (booke_wdt_enabled == 1) {
  153. printk(KERN_INFO
  154. "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  155. booke_wdt_period);
  156. on_each_cpu(__booke_wdt_enable, NULL, 0);
  157. }
  158. spin_unlock(&booke_wdt_lock);
  159. return ret;
  160. }
  161. device_initcall(booke_wdt_init);