designware_wdt.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2013 Altera Corporation <www.altera.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <watchdog.h>
  8. #include <asm/io.h>
  9. #include <asm/utils.h>
  10. #define DW_WDT_CR 0x00
  11. #define DW_WDT_TORR 0x04
  12. #define DW_WDT_CRR 0x0C
  13. #define DW_WDT_CR_EN_OFFSET 0x00
  14. #define DW_WDT_CR_RMOD_OFFSET 0x01
  15. #define DW_WDT_CR_RMOD_VAL 0x00
  16. #define DW_WDT_CRR_RESTART_VAL 0x76
  17. /*
  18. * Set the watchdog time interval.
  19. * Counter is 32 bit.
  20. */
  21. static int designware_wdt_settimeout(unsigned int timeout)
  22. {
  23. signed int i;
  24. /* calculate the timeout range value */
  25. i = (log_2_n_round_up(timeout * CONFIG_DW_WDT_CLOCK_KHZ)) - 16;
  26. if (i > 15)
  27. i = 15;
  28. if (i < 0)
  29. i = 0;
  30. writel((i | (i << 4)), (CONFIG_DW_WDT_BASE + DW_WDT_TORR));
  31. return 0;
  32. }
  33. static void designware_wdt_enable(void)
  34. {
  35. writel(((DW_WDT_CR_RMOD_VAL << DW_WDT_CR_RMOD_OFFSET) |
  36. (0x1 << DW_WDT_CR_EN_OFFSET)),
  37. (CONFIG_DW_WDT_BASE + DW_WDT_CR));
  38. }
  39. static unsigned int designware_wdt_is_enabled(void)
  40. {
  41. unsigned long val;
  42. val = readl((CONFIG_DW_WDT_BASE + DW_WDT_CR));
  43. return val & 0x1;
  44. }
  45. #if defined(CONFIG_HW_WATCHDOG)
  46. void hw_watchdog_reset(void)
  47. {
  48. if (designware_wdt_is_enabled())
  49. /* restart the watchdog counter */
  50. writel(DW_WDT_CRR_RESTART_VAL,
  51. (CONFIG_DW_WDT_BASE + DW_WDT_CRR));
  52. }
  53. void hw_watchdog_init(void)
  54. {
  55. /* reset to disable the watchdog */
  56. hw_watchdog_reset();
  57. /* set timer in miliseconds */
  58. designware_wdt_settimeout(CONFIG_HW_WATCHDOG_TIMEOUT_MS);
  59. /* enable the watchdog */
  60. designware_wdt_enable();
  61. /* reset the watchdog */
  62. hw_watchdog_reset();
  63. }
  64. #endif