booke_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * drivers/char/watchdog/booke_wdt.c
  3. *
  4. * Watchdog timer for PowerPC Book-E systems
  5. *
  6. * Author: Matthew McClintock
  7. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  8. *
  9. * Copyright 2005 Freescale Semiconductor Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/notifier.h>
  20. #include <linux/watchdog.h>
  21. #include <asm/reg_booke.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/system.h>
  24. /* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
  25. * Also, the wdt_period sets the watchdog timer period timeout.
  26. * For E500 cpus the wdt_period sets which bit changing from 0->1 will
  27. * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
  28. * first time nothing will happen, the second time a watchdog exception will
  29. * occur, and the final time the board will reset.
  30. */
  31. #ifdef CONFIG_FSL_BOOKE
  32. #define WDT_PERIOD_DEFAULT 63 /* Ex. wdt_period=28 bus=333Mhz , reset=~40sec */
  33. #else
  34. #define WDT_PERIOD_DEFAULT 3 /* Refer to the PPC40x and PPC4xx manuals */
  35. #endif /* for timing information */
  36. u32 booke_wdt_enabled = 0;
  37. u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
  38. #ifdef CONFIG_FSL_BOOKE
  39. #define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
  40. #else
  41. #define WDTP(x) (TCR_WP(x))
  42. #endif
  43. /*
  44. * booke_wdt_ping:
  45. */
  46. static __inline__ void booke_wdt_ping(void)
  47. {
  48. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  49. }
  50. /*
  51. * booke_wdt_enable:
  52. */
  53. static __inline__ void booke_wdt_enable(void)
  54. {
  55. u32 val;
  56. /* clear status before enabling watchdog */
  57. booke_wdt_ping();
  58. val = mfspr(SPRN_TCR);
  59. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  60. mtspr(SPRN_TCR, val);
  61. }
  62. /*
  63. * booke_wdt_write:
  64. */
  65. static ssize_t booke_wdt_write (struct file *file, const char __user *buf,
  66. size_t count, loff_t *ppos)
  67. {
  68. booke_wdt_ping();
  69. return count;
  70. }
  71. static struct watchdog_info ident = {
  72. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  73. .firmware_version = 0,
  74. .identity = "PowerPC Book-E Watchdog",
  75. };
  76. /*
  77. * booke_wdt_ioctl:
  78. */
  79. static int booke_wdt_ioctl (struct inode *inode, struct file *file,
  80. unsigned int cmd, unsigned long arg)
  81. {
  82. u32 tmp = 0;
  83. u32 __user *p = (u32 __user *)arg;
  84. switch (cmd) {
  85. case WDIOC_GETSUPPORT:
  86. if (copy_to_user ((struct watchdog_info __user *) arg, &ident,
  87. sizeof(struct watchdog_info)))
  88. return -EFAULT;
  89. case WDIOC_GETSTATUS:
  90. return put_user(ident.options, p);
  91. case WDIOC_GETBOOTSTATUS:
  92. /* XXX: something is clearing TSR */
  93. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  94. /* returns 1 if last reset was caused by the WDT */
  95. return (tmp ? 1 : 0);
  96. case WDIOC_KEEPALIVE:
  97. booke_wdt_ping();
  98. return 0;
  99. case WDIOC_SETTIMEOUT:
  100. if (get_user(booke_wdt_period, p))
  101. return -EFAULT;
  102. mtspr(SPRN_TCR, (mfspr(SPRN_TCR)&~WDTP(0))|WDTP(booke_wdt_period));
  103. return 0;
  104. case WDIOC_GETTIMEOUT:
  105. return put_user(booke_wdt_period, p);
  106. case WDIOC_SETOPTIONS:
  107. if (get_user(tmp, p))
  108. return -EINVAL;
  109. if (tmp == WDIOS_ENABLECARD) {
  110. booke_wdt_ping();
  111. break;
  112. } else
  113. return -EINVAL;
  114. return 0;
  115. default:
  116. return -ENOTTY;
  117. }
  118. return 0;
  119. }
  120. /*
  121. * booke_wdt_open:
  122. */
  123. static int booke_wdt_open (struct inode *inode, struct file *file)
  124. {
  125. if (booke_wdt_enabled == 0) {
  126. booke_wdt_enabled = 1;
  127. booke_wdt_enable();
  128. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  129. booke_wdt_period);
  130. }
  131. return nonseekable_open(inode, file);
  132. }
  133. static const struct file_operations booke_wdt_fops = {
  134. .owner = THIS_MODULE,
  135. .llseek = no_llseek,
  136. .write = booke_wdt_write,
  137. .ioctl = booke_wdt_ioctl,
  138. .open = booke_wdt_open,
  139. };
  140. static struct miscdevice booke_wdt_miscdev = {
  141. .minor = WATCHDOG_MINOR,
  142. .name = "watchdog",
  143. .fops = &booke_wdt_fops,
  144. };
  145. static void __exit booke_wdt_exit(void)
  146. {
  147. misc_deregister(&booke_wdt_miscdev);
  148. }
  149. /*
  150. * booke_wdt_init:
  151. */
  152. static int __init booke_wdt_init(void)
  153. {
  154. int ret = 0;
  155. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  156. ident.firmware_version = cur_cpu_spec->pvr_value;
  157. ret = misc_register(&booke_wdt_miscdev);
  158. if (ret) {
  159. printk (KERN_CRIT "Cannot register miscdev on minor=%d (err=%d)\n",
  160. WATCHDOG_MINOR, ret);
  161. return ret;
  162. }
  163. if (booke_wdt_enabled == 1) {
  164. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  165. booke_wdt_period);
  166. booke_wdt_enable();
  167. }
  168. return ret;
  169. }
  170. device_initcall(booke_wdt_init);