cortina_wdt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Cortina-Access
  4. *
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <hang.h>
  9. #include <asm/io.h>
  10. #include <wdt.h>
  11. #include <linux/bitops.h>
  12. #define CA_WDT_CTRL 0x00
  13. #define CA_WDT_PS 0x04
  14. #define CA_WDT_DIV 0x08
  15. #define CA_WDT_LD 0x0C
  16. #define CA_WDT_LOADE 0x10
  17. #define CA_WDT_CNT 0x14
  18. #define CA_WDT_IE 0x18
  19. #define CA_WDT_INT 0x1C
  20. #define CA_WDT_STAT 0x20
  21. /* CA_WDT_CTRL */
  22. #define CTL_WDT_EN BIT(0)
  23. #define CTL_WDT_RSTEN BIT(1)
  24. #define CTL_WDT_CLK_SEL BIT(2)
  25. /* CA_WDT_LOADE */
  26. #define WDT_UPD BIT(0)
  27. #define WDT_UPD_PS BIT(1)
  28. /* Global config */
  29. #define WDT_RESET_SUB BIT(4)
  30. #define WDT_RESET_ALL_BLOCK BIT(6)
  31. #define WDT_RESET_REMAP BIT(7)
  32. #define WDT_EXT_RESET BIT(8)
  33. #define WDT_RESET_DEFAULT (WDT_EXT_RESET | WDT_RESET_REMAP | \
  34. WDT_RESET_ALL_BLOCK | WDT_RESET_SUB)
  35. struct ca_wdt_priv {
  36. void __iomem *base;
  37. void __iomem *global_config;
  38. };
  39. static void cortina_wdt_set_timeout(struct udevice *dev, u64 timeout_ms)
  40. {
  41. struct ca_wdt_priv *priv = dev_get_priv(dev);
  42. /* Prescale using millisecond unit */
  43. writel(CORTINA_PER_IO_FREQ / 1000, priv->base + CA_WDT_PS);
  44. /* Millisecond */
  45. writel(1, priv->base + CA_WDT_DIV);
  46. writel(timeout_ms, priv->base + CA_WDT_LD);
  47. writel(WDT_UPD | WDT_UPD_PS, priv->base + CA_WDT_LOADE);
  48. }
  49. static int cortina_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
  50. {
  51. struct ca_wdt_priv *priv = dev_get_priv(dev);
  52. cortina_wdt_set_timeout(dev, timeout);
  53. /* WDT Reset option */
  54. setbits_32(priv->global_config, WDT_RESET_DEFAULT);
  55. /* Enable WDT */
  56. setbits_32(priv->base, CTL_WDT_EN | CTL_WDT_RSTEN | CTL_WDT_CLK_SEL);
  57. return 0;
  58. }
  59. static int cortina_wdt_stop(struct udevice *dev)
  60. {
  61. struct ca_wdt_priv *priv = dev_get_priv(dev);
  62. /* Disable WDT */
  63. writel(0, priv->base);
  64. return 0;
  65. }
  66. static int cortina_wdt_reset(struct udevice *dev)
  67. {
  68. struct ca_wdt_priv *priv = dev_get_priv(dev);
  69. /* Reload WDT counter */
  70. writel(WDT_UPD, priv->base + CA_WDT_LOADE);
  71. return 0;
  72. }
  73. static int cortina_wdt_expire_now(struct udevice *dev, ulong flags)
  74. {
  75. /* Set 1ms timeout to reset system */
  76. cortina_wdt_set_timeout(dev, 1);
  77. hang();
  78. return 0;
  79. }
  80. static int cortina_wdt_probe(struct udevice *dev)
  81. {
  82. struct ca_wdt_priv *priv = dev_get_priv(dev);
  83. priv->base = dev_remap_addr_index(dev, 0);
  84. if (!priv->base)
  85. return -ENOENT;
  86. priv->global_config = dev_remap_addr_index(dev, 1);
  87. if (!priv->global_config)
  88. return -ENOENT;
  89. /* Stop WDT */
  90. cortina_wdt_stop(dev);
  91. return 0;
  92. }
  93. static const struct wdt_ops cortina_wdt_ops = {
  94. .start = cortina_wdt_start,
  95. .reset = cortina_wdt_reset,
  96. .stop = cortina_wdt_stop,
  97. .expire_now = cortina_wdt_expire_now,
  98. };
  99. static const struct udevice_id cortina_wdt_ids[] = {
  100. {.compatible = "cortina,ca-wdt"},
  101. {}
  102. };
  103. U_BOOT_DRIVER(cortina_wdt) = {
  104. .name = "cortina_wdt",
  105. .id = UCLASS_WDT,
  106. .probe = cortina_wdt_probe,
  107. .of_match = cortina_wdt_ids,
  108. .ops = &cortina_wdt_ops,
  109. };