booke_wdt.c 5.8 KB

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