at91sam9_wdt.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]
  3. *
  4. * Watchdog driver for Atmel AT91SAM9x processors.
  5. *
  6. * Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  7. * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. /*
  12. * The Watchdog Timer Mode Register can be only written to once. If the
  13. * timeout need to be set from U-Boot, be sure that the bootstrap doesn't
  14. * write to this register. Inform Linux to it too
  15. */
  16. #include <common.h>
  17. #include <watchdog.h>
  18. #include <asm/arch/hardware.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/at91_wdt.h>
  21. /*
  22. * AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
  23. * use this to convert a watchdog
  24. * value from/to milliseconds.
  25. */
  26. #define ms_to_ticks(t) (((t << 8) / 1000) - 1)
  27. #define ticks_to_ms(t) (((t + 1) * 1000) >> 8)
  28. /* Hardware timeout in seconds */
  29. #define WDT_HW_TIMEOUT 2
  30. /*
  31. * Set the watchdog time interval in 1/256Hz (write-once)
  32. * Counter is 12 bit.
  33. */
  34. static int at91_wdt_settimeout(unsigned int timeout)
  35. {
  36. unsigned int reg;
  37. at91_wdt_t *wd = (at91_wdt_t *) ATMEL_BASE_WDT;
  38. /* Check if disabled */
  39. if (readl(&wd->mr) & AT91_WDT_MR_WDDIS) {
  40. printf("sorry, watchdog is disabled\n");
  41. return -1;
  42. }
  43. /*
  44. * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
  45. *
  46. * Since WDV is a 12-bit counter, the maximum period is
  47. * 4096 / 256 = 16 seconds.
  48. */
  49. reg = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */
  50. | AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */
  51. | AT91_WDT_MR_WDD(0xfff) /* restart at any time */
  52. | AT91_WDT_MR_WDV(timeout); /* timer value */
  53. writel(reg, &wd->mr);
  54. return 0;
  55. }
  56. void hw_watchdog_reset(void)
  57. {
  58. at91_wdt_t *wd = (at91_wdt_t *) ATMEL_BASE_WDT;
  59. writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, &wd->cr);
  60. }
  61. void hw_watchdog_init(void)
  62. {
  63. /* 16 seconds timer, resets enabled */
  64. at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));
  65. }