booke_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, 2010-2011 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/smp.h>
  17. #include <linux/watchdog.h>
  18. #include <asm/reg_booke.h>
  19. #include <asm/time.h>
  20. #include <asm/div64.h>
  21. /* If the kernel parameter wdt=1, the watchdog will be enabled at boot.
  22. * Also, the wdt_period sets the watchdog timer period timeout.
  23. * For E500 cpus the wdt_period sets which bit changing from 0->1 will
  24. * trigger a watchog timeout. This watchdog timeout will occur 3 times, the
  25. * first time nothing will happen, the second time a watchdog exception will
  26. * occur, and the final time the board will reset.
  27. */
  28. #ifdef CONFIG_PPC_FSL_BOOK3E
  29. #define WDTP(x) ((((x)&0x3)<<30)|(((x)&0x3c)<<15))
  30. #define WDTP_MASK (WDTP(0x3f))
  31. #else
  32. #define WDTP(x) (TCR_WP(x))
  33. #define WDTP_MASK (TCR_WP_MASK)
  34. #endif
  35. static bool booke_wdt_enabled;
  36. module_param(booke_wdt_enabled, bool, 0);
  37. static int booke_wdt_period = CONFIG_BOOKE_WDT_DEFAULT_TIMEOUT;
  38. module_param(booke_wdt_period, int, 0);
  39. #ifdef CONFIG_PPC_FSL_BOOK3E
  40. /* For the specified period, determine the number of seconds
  41. * corresponding to the reset time. There will be a watchdog
  42. * exception at approximately 3/5 of this time.
  43. *
  44. * The formula to calculate this is given by:
  45. * 2.5 * (2^(63-period+1)) / timebase_freq
  46. *
  47. * In order to simplify things, we assume that period is
  48. * at least 1. This will still result in a very long timeout.
  49. */
  50. static unsigned long long period_to_sec(unsigned int period)
  51. {
  52. unsigned long long tmp = 1ULL << (64 - period);
  53. unsigned long tmp2 = ppc_tb_freq;
  54. /* tmp may be a very large number and we don't want to overflow,
  55. * so divide the timebase freq instead of multiplying tmp
  56. */
  57. tmp2 = tmp2 / 5 * 2;
  58. do_div(tmp, tmp2);
  59. return tmp;
  60. }
  61. /*
  62. * This procedure will find the highest period which will give a timeout
  63. * greater than the one required. e.g. for a bus speed of 66666666 and
  64. * and a parameter of 2 secs, then this procedure will return a value of 38.
  65. */
  66. static unsigned int sec_to_period(unsigned int secs)
  67. {
  68. unsigned int period;
  69. for (period = 63; period > 0; period--) {
  70. if (period_to_sec(period) >= secs)
  71. return period;
  72. }
  73. return 0;
  74. }
  75. #define MAX_WDT_TIMEOUT period_to_sec(1)
  76. #else /* CONFIG_PPC_FSL_BOOK3E */
  77. static unsigned long long period_to_sec(unsigned int period)
  78. {
  79. return period;
  80. }
  81. static unsigned int sec_to_period(unsigned int secs)
  82. {
  83. return secs;
  84. }
  85. #define MAX_WDT_TIMEOUT 3 /* from Kconfig */
  86. #endif /* !CONFIG_PPC_FSL_BOOK3E */
  87. static void __booke_wdt_set(void *data)
  88. {
  89. u32 val;
  90. struct watchdog_device *wdog = data;
  91. val = mfspr(SPRN_TCR);
  92. val &= ~WDTP_MASK;
  93. val |= WDTP(sec_to_period(wdog->timeout));
  94. mtspr(SPRN_TCR, val);
  95. }
  96. static void booke_wdt_set(void *data)
  97. {
  98. on_each_cpu(__booke_wdt_set, data, 0);
  99. }
  100. static void __booke_wdt_ping(void *data)
  101. {
  102. mtspr(SPRN_TSR, TSR_ENW|TSR_WIS);
  103. }
  104. static int booke_wdt_ping(struct watchdog_device *wdog)
  105. {
  106. on_each_cpu(__booke_wdt_ping, NULL, 0);
  107. return 0;
  108. }
  109. static void __booke_wdt_enable(void *data)
  110. {
  111. u32 val;
  112. struct watchdog_device *wdog = data;
  113. /* clear status before enabling watchdog */
  114. __booke_wdt_ping(NULL);
  115. val = mfspr(SPRN_TCR);
  116. val &= ~WDTP_MASK;
  117. val |= (TCR_WIE|TCR_WRC(WRC_CHIP)|WDTP(sec_to_period(wdog->timeout)));
  118. mtspr(SPRN_TCR, val);
  119. }
  120. /**
  121. * booke_wdt_disable - disable the watchdog on the given CPU
  122. *
  123. * This function is called on each CPU. It disables the watchdog on that CPU.
  124. *
  125. * TCR[WRC] cannot be changed once it has been set to non-zero, but we can
  126. * effectively disable the watchdog by setting its period to the maximum value.
  127. */
  128. static void __booke_wdt_disable(void *data)
  129. {
  130. u32 val;
  131. val = mfspr(SPRN_TCR);
  132. val &= ~(TCR_WIE | WDTP_MASK);
  133. mtspr(SPRN_TCR, val);
  134. /* clear status to make sure nothing is pending */
  135. __booke_wdt_ping(NULL);
  136. }
  137. static int booke_wdt_start(struct watchdog_device *wdog)
  138. {
  139. on_each_cpu(__booke_wdt_enable, wdog, 0);
  140. pr_debug("watchdog enabled (timeout = %u sec)\n", wdog->timeout);
  141. return 0;
  142. }
  143. static int booke_wdt_stop(struct watchdog_device *wdog)
  144. {
  145. on_each_cpu(__booke_wdt_disable, NULL, 0);
  146. pr_debug("watchdog disabled\n");
  147. return 0;
  148. }
  149. static int booke_wdt_set_timeout(struct watchdog_device *wdt_dev,
  150. unsigned int timeout)
  151. {
  152. wdt_dev->timeout = timeout;
  153. booke_wdt_set(wdt_dev);
  154. return 0;
  155. }
  156. static struct watchdog_info booke_wdt_info __ro_after_init = {
  157. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  158. .identity = "PowerPC Book-E Watchdog",
  159. };
  160. static const struct watchdog_ops booke_wdt_ops = {
  161. .owner = THIS_MODULE,
  162. .start = booke_wdt_start,
  163. .stop = booke_wdt_stop,
  164. .ping = booke_wdt_ping,
  165. .set_timeout = booke_wdt_set_timeout,
  166. };
  167. static struct watchdog_device booke_wdt_dev = {
  168. .info = &booke_wdt_info,
  169. .ops = &booke_wdt_ops,
  170. .min_timeout = 1,
  171. };
  172. static void __exit booke_wdt_exit(void)
  173. {
  174. watchdog_unregister_device(&booke_wdt_dev);
  175. }
  176. static int __init booke_wdt_init(void)
  177. {
  178. int ret = 0;
  179. bool nowayout = WATCHDOG_NOWAYOUT;
  180. pr_info("powerpc book-e watchdog driver loaded\n");
  181. booke_wdt_info.firmware_version = cur_cpu_spec->pvr_value;
  182. booke_wdt_set_timeout(&booke_wdt_dev,
  183. period_to_sec(booke_wdt_period));
  184. watchdog_set_nowayout(&booke_wdt_dev, nowayout);
  185. booke_wdt_dev.max_timeout = MAX_WDT_TIMEOUT;
  186. if (booke_wdt_enabled)
  187. booke_wdt_start(&booke_wdt_dev);
  188. ret = watchdog_register_device(&booke_wdt_dev);
  189. return ret;
  190. }
  191. module_init(booke_wdt_init);
  192. module_exit(booke_wdt_exit);
  193. MODULE_ALIAS("booke_wdt");
  194. MODULE_DESCRIPTION("PowerPC Book-E watchdog driver");
  195. MODULE_LICENSE("GPL");