booke_wdt.c 4.4 KB

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