booke_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <asm/reg_booke.h>
  21. #include <asm/uaccess.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, 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 int booke_wdt_ioctl(struct inode *inode, 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((struct watchdog_info __user *)arg, &ident,
  78. sizeof(struct watchdog_info)))
  79. return -EFAULT;
  80. case WDIOC_GETSTATUS:
  81. return put_user(ident.options, p);
  82. case WDIOC_GETBOOTSTATUS:
  83. /* XXX: something is clearing TSR */
  84. tmp = mfspr(SPRN_TSR) & TSR_WRS(3);
  85. /* returns 1 if last reset was caused by the WDT */
  86. return (tmp ? 1 : 0);
  87. case WDIOC_KEEPALIVE:
  88. booke_wdt_ping();
  89. return 0;
  90. case WDIOC_SETTIMEOUT:
  91. if (get_user(booke_wdt_period, p))
  92. return -EFAULT;
  93. mtspr(SPRN_TCR, (mfspr(SPRN_TCR)&~WDTP(0))|WDTP(booke_wdt_period));
  94. return 0;
  95. case WDIOC_GETTIMEOUT:
  96. return put_user(booke_wdt_period, p);
  97. case WDIOC_SETOPTIONS:
  98. if (get_user(tmp, p))
  99. return -EINVAL;
  100. if (tmp == WDIOS_ENABLECARD) {
  101. booke_wdt_ping();
  102. break;
  103. } else
  104. return -EINVAL;
  105. return 0;
  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, 0);
  117. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Enabled "
  118. "(wdt_period=%d)\n", booke_wdt_period);
  119. }
  120. spin_unlock(&booke_wdt_lock);
  121. return nonseekable_open(inode, file);
  122. }
  123. static const struct file_operations booke_wdt_fops = {
  124. .owner = THIS_MODULE,
  125. .llseek = no_llseek,
  126. .write = booke_wdt_write,
  127. .ioctl = booke_wdt_ioctl,
  128. .open = booke_wdt_open,
  129. };
  130. static struct miscdevice booke_wdt_miscdev = {
  131. .minor = WATCHDOG_MINOR,
  132. .name = "watchdog",
  133. .fops = &booke_wdt_fops,
  134. };
  135. static void __exit booke_wdt_exit(void)
  136. {
  137. misc_deregister(&booke_wdt_miscdev);
  138. }
  139. static int __init booke_wdt_init(void)
  140. {
  141. int ret = 0;
  142. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
  143. ident.firmware_version = cur_cpu_spec->pvr_value;
  144. ret = misc_register(&booke_wdt_miscdev);
  145. if (ret) {
  146. printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
  147. WATCHDOG_MINOR, ret);
  148. return ret;
  149. }
  150. spin_lock(&booke_wdt_lock);
  151. if (booke_wdt_enabled == 1) {
  152. printk(KERN_INFO "PowerPC Book-E Watchdog Timer Enabled "
  153. "(wdt_period=%d)\n", booke_wdt_period);
  154. on_each_cpu(__booke_wdt_enable, NULL, 0, 0);
  155. }
  156. spin_unlock(&booke_wdt_lock);
  157. return ret;
  158. }
  159. device_initcall(booke_wdt_init);