booke_wdt.c 5.6 KB

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