booke_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/config.h>
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/notifier.h>
  21. #include <linux/watchdog.h>
  22. #include <asm/reg_booke.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/system.h>
  25. /* If the kernel parameter wdt_enable=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 63 /* Ex. wdt_period=28 bus=333Mhz , reset=~40sec */
  34. #else
  35. #define WDT_PERIOD_DEFAULT 4 /* Refer to the PPC40x and PPC4xx manuals */
  36. #endif /* for timing information */
  37. u32 booke_wdt_enabled = 0;
  38. u32 booke_wdt_period = WDT_PERIOD_DEFAULT;
  39. #ifdef CONFIG_FSL_BOOKE
  40. #define WDTP(x) ((((63-x)&0x3)<<30)|(((63-x)&0x3c)<<15))
  41. #else
  42. #define WDTP(x) (TCR_WP(x))
  43. #endif
  44. /*
  45. * booke_wdt_enable:
  46. */
  47. static __inline__ void booke_wdt_enable(void)
  48. {
  49. u32 val;
  50. val = mfspr(SPRN_TCR);
  51. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(booke_wdt_period));
  52. mtspr(SPRN_TCR, val);
  53. }
  54. /*
  55. * booke_wdt_ping:
  56. */
  57. static __inline__ void booke_wdt_ping(void)
  58. {
  59. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  60. }
  61. /*
  62. * booke_wdt_write:
  63. */
  64. static ssize_t booke_wdt_write (struct file *file, const char __user *buf,
  65. size_t count, loff_t *ppos)
  66. {
  67. booke_wdt_ping();
  68. return count;
  69. }
  70. static struct watchdog_info ident = {
  71. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  72. .firmware_version = 0,
  73. .identity = "PowerPC Book-E Watchdog",
  74. };
  75. /*
  76. * booke_wdt_ioctl:
  77. */
  78. static int booke_wdt_ioctl (struct inode *inode, struct file *file,
  79. unsigned int cmd, unsigned long arg)
  80. {
  81. u32 tmp = 0;
  82. u32 __user *p = (u32 __user *)arg;
  83. switch (cmd) {
  84. case WDIOC_GETSUPPORT:
  85. if (copy_to_user ((struct watchdog_info __user *) arg, &ident,
  86. sizeof(struct watchdog_info)))
  87. return -EFAULT;
  88. case WDIOC_GETSTATUS:
  89. return put_user(ident.options, p);
  90. case WDIOC_GETBOOTSTATUS:
  91. /* XXX: something is clearing TSR */
  92. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  93. /* returns 1 if last reset was caused by the WDT */
  94. return (tmp ? 1 : 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))|WDTP(booke_wdt_period));
  102. return 0;
  103. case WDIOC_GETTIMEOUT:
  104. return put_user(booke_wdt_period, p);
  105. case WDIOC_SETOPTIONS:
  106. if (get_user(tmp, p))
  107. return -EINVAL;
  108. if (tmp == WDIOS_ENABLECARD) {
  109. booke_wdt_ping();
  110. break;
  111. } else
  112. return -EINVAL;
  113. return 0;
  114. default:
  115. return -ENOIOCTLCMD;
  116. }
  117. return 0;
  118. }
  119. /*
  120. * booke_wdt_open:
  121. */
  122. static int booke_wdt_open (struct inode *inode, struct file *file)
  123. {
  124. if (booke_wdt_enabled == 0) {
  125. booke_wdt_enabled = 1;
  126. booke_wdt_enable();
  127. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  128. booke_wdt_period);
  129. }
  130. return 0;
  131. }
  132. static struct file_operations booke_wdt_fops = {
  133. .owner = THIS_MODULE,
  134. .llseek = no_llseek,
  135. .write = booke_wdt_write,
  136. .ioctl = booke_wdt_ioctl,
  137. .open = booke_wdt_open,
  138. };
  139. static struct miscdevice booke_wdt_miscdev = {
  140. .minor = WATCHDOG_MINOR,
  141. .name = "watchdog",
  142. .fops = &booke_wdt_fops,
  143. };
  144. static void __exit booke_wdt_exit(void)
  145. {
  146. misc_deregister(&booke_wdt_miscdev);
  147. }
  148. /*
  149. * booke_wdt_init:
  150. */
  151. static int __init booke_wdt_init(void)
  152. {
  153. int ret = 0;
  154. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  155. ident.firmware_version = cur_cpu_spec->pvr_value;
  156. ret = misc_register(&booke_wdt_miscdev);
  157. if (ret) {
  158. printk (KERN_CRIT "Cannot register miscdev on minor=%d (err=%d)\n",
  159. WATCHDOG_MINOR, ret);
  160. return ret;
  161. }
  162. if (booke_wdt_enabled == 1) {
  163. printk (KERN_INFO "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
  164. booke_wdt_period);
  165. booke_wdt_enable();
  166. }
  167. return ret;
  168. }
  169. device_initcall(booke_wdt_init);