imx_watchdog.c 3.5 KB

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