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 <kumar.gala@freescale.com>
  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 *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. switch (cmd) {
  83. case WDIOC_GETSUPPORT:
  84. if (copy_to_user ((struct watchdog_info *) arg, &ident,
  85. sizeof(struct watchdog_info)))
  86. return -EFAULT;
  87. case WDIOC_GETSTATUS:
  88. return put_user(ident.options, (u32 *) arg);
  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, (u32 *) arg))
  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, (u32 *) arg);
  104. case WDIOC_SETOPTIONS:
  105. if (get_user(tmp, (u32 *) arg))
  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 -ENOIOCTLCMD;
  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 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 = cpu_specs[0].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);