imx_watchdog.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * watchdog.c - driver for i.mx on-chip watchdog
  3. *
  4. * Licensed under the GPL-2 or later.
  5. */
  6. #include <common.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <hang.h>
  10. #include <asm/io.h>
  11. #include <wdt.h>
  12. #include <watchdog.h>
  13. #include <asm/arch/imx-regs.h>
  14. #ifdef CONFIG_FSL_LSCH2
  15. #include <asm/arch/immap_lsch2.h>
  16. #endif
  17. #include <fsl_wdog.h>
  18. static void imx_watchdog_expire_now(struct watchdog_regs *wdog, bool ext_reset)
  19. {
  20. u16 wcr = WCR_WDE;
  21. if (ext_reset)
  22. wcr |= WCR_SRS; /* do not assert internal reset */
  23. else
  24. wcr |= WCR_WDA; /* do not assert external reset */
  25. /* Write 3 times to ensure it works, due to IMX6Q errata ERR004346 */
  26. writew(wcr, &wdog->wcr);
  27. writew(wcr, &wdog->wcr);
  28. writew(wcr, &wdog->wcr);
  29. while (1) {
  30. /*
  31. * spin before reset
  32. */
  33. }
  34. }
  35. #if !defined(CONFIG_IMX_WATCHDOG) || \
  36. (defined(CONFIG_IMX_WATCHDOG) && !CONFIG_IS_ENABLED(WDT))
  37. void __attribute__((weak)) reset_cpu(ulong addr)
  38. {
  39. struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
  40. imx_watchdog_expire_now(wdog, true);
  41. }
  42. #endif
  43. #if defined(CONFIG_IMX_WATCHDOG)
  44. static void imx_watchdog_reset(struct watchdog_regs *wdog)
  45. {
  46. #ifndef CONFIG_WATCHDOG_RESET_DISABLE
  47. writew(0x5555, &wdog->wsr);
  48. writew(0xaaaa, &wdog->wsr);
  49. #endif /* CONFIG_WATCHDOG_RESET_DISABLE*/
  50. }
  51. static void imx_watchdog_init(struct watchdog_regs *wdog, bool ext_reset)
  52. {
  53. u16 timeout;
  54. u16 wcr;
  55. /*
  56. * The timer watchdog can be set between
  57. * 0.5 and 128 Seconds. If not defined
  58. * in configuration file, sets 128 Seconds
  59. */
  60. #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS
  61. #define CONFIG_WATCHDOG_TIMEOUT_MSECS 128000
  62. #endif
  63. timeout = (CONFIG_WATCHDOG_TIMEOUT_MSECS / 500) - 1;
  64. #ifdef CONFIG_FSL_LSCH2
  65. wcr = (WCR_WDA | WCR_SRS | WCR_WDE) << 8 | timeout;
  66. #else
  67. wcr = WCR_WDZST | WCR_WDBG | WCR_WDE | WCR_SRS |
  68. WCR_WDA | SET_WCR_WT(timeout);
  69. if (ext_reset)
  70. wcr |= WCR_WDT;
  71. #endif /* CONFIG_FSL_LSCH2*/
  72. writew(wcr, &wdog->wcr);
  73. imx_watchdog_reset(wdog);
  74. }
  75. #if !CONFIG_IS_ENABLED(WDT)
  76. void hw_watchdog_reset(void)
  77. {
  78. struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
  79. imx_watchdog_reset(wdog);
  80. }
  81. void hw_watchdog_init(void)
  82. {
  83. struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
  84. imx_watchdog_init(wdog, true);
  85. }
  86. #else
  87. struct imx_wdt_priv {
  88. void __iomem *base;
  89. bool ext_reset;
  90. };
  91. static int imx_wdt_reset(struct udevice *dev)
  92. {
  93. struct imx_wdt_priv *priv = dev_get_priv(dev);
  94. imx_watchdog_reset(priv->base);
  95. return 0;
  96. }
  97. static int imx_wdt_expire_now(struct udevice *dev, ulong flags)
  98. {
  99. struct imx_wdt_priv *priv = dev_get_priv(dev);
  100. imx_watchdog_expire_now(priv->base, priv->ext_reset);
  101. hang();
  102. return 0;
  103. }
  104. static int imx_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
  105. {
  106. struct imx_wdt_priv *priv = dev_get_priv(dev);
  107. imx_watchdog_init(priv->base, priv->ext_reset);
  108. return 0;
  109. }
  110. static int imx_wdt_probe(struct udevice *dev)
  111. {
  112. struct imx_wdt_priv *priv = dev_get_priv(dev);
  113. priv->base = dev_read_addr_ptr(dev);
  114. if (!priv->base)
  115. return -ENOENT;
  116. priv->ext_reset = dev_read_bool(dev, "fsl,ext-reset-output");
  117. return 0;
  118. }
  119. static const struct wdt_ops imx_wdt_ops = {
  120. .start = imx_wdt_start,
  121. .reset = imx_wdt_reset,
  122. .expire_now = imx_wdt_expire_now,
  123. };
  124. static const struct udevice_id imx_wdt_ids[] = {
  125. { .compatible = "fsl,imx21-wdt" },
  126. {}
  127. };
  128. U_BOOT_DRIVER(imx_wdt) = {
  129. .name = "imx_wdt",
  130. .id = UCLASS_WDT,
  131. .of_match = imx_wdt_ids,
  132. .probe = imx_wdt_probe,
  133. .ops = &imx_wdt_ops,
  134. .priv_auto_alloc_size = sizeof(struct imx_wdt_priv),
  135. .flags = DM_FLAG_PRE_RELOC,
  136. };
  137. #endif
  138. #endif