orion_wdt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * drivers/watchdog/orion_wdt.c
  3. *
  4. * Watchdog driver for Orion/Kirkwood processors
  5. *
  6. * Authors: Tomas Hlavacek <tmshlvck@gmail.com>
  7. * Sylver Bruneau <sylver.bruneau@googlemail.com>
  8. * Marek Behun <marek.behun@nic.cz>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <clk.h>
  17. #include <wdt.h>
  18. #include <linux/kernel.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/cpu.h>
  21. #include <asm/arch/soc.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. struct orion_wdt_priv {
  24. void __iomem *reg;
  25. int wdt_counter_offset;
  26. void __iomem *rstout;
  27. void __iomem *rstout_mask;
  28. u32 timeout;
  29. unsigned long clk_rate;
  30. struct clk clk;
  31. };
  32. #define RSTOUT_ENABLE_BIT BIT(8)
  33. #define RSTOUT_MASK_BIT BIT(10)
  34. #define WDT_ENABLE_BIT BIT(8)
  35. #define TIMER_CTRL 0x0000
  36. #define TIMER_A370_STATUS 0x04
  37. #define WDT_AXP_FIXED_ENABLE_BIT BIT(10)
  38. #define WDT_A370_EXPIRED BIT(31)
  39. static int orion_wdt_reset(struct udevice *dev)
  40. {
  41. struct orion_wdt_priv *priv = dev_get_priv(dev);
  42. /* Reload watchdog duration */
  43. writel(priv->clk_rate * priv->timeout,
  44. priv->reg + priv->wdt_counter_offset);
  45. return 0;
  46. }
  47. static int orion_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
  48. {
  49. struct orion_wdt_priv *priv = dev_get_priv(dev);
  50. u32 reg;
  51. priv->timeout = DIV_ROUND_UP(timeout_ms, 1000);
  52. /* Enable the fixed watchdog clock input */
  53. reg = readl(priv->reg + TIMER_CTRL);
  54. reg |= WDT_AXP_FIXED_ENABLE_BIT;
  55. writel(reg, priv->reg + TIMER_CTRL);
  56. /* Set watchdog duration */
  57. writel(priv->clk_rate * priv->timeout,
  58. priv->reg + priv->wdt_counter_offset);
  59. /* Clear the watchdog expiration bit */
  60. reg = readl(priv->reg + TIMER_A370_STATUS);
  61. reg &= ~WDT_A370_EXPIRED;
  62. writel(reg, priv->reg + TIMER_A370_STATUS);
  63. /* Enable watchdog timer */
  64. reg = readl(priv->reg + TIMER_CTRL);
  65. reg |= WDT_ENABLE_BIT;
  66. writel(reg, priv->reg + TIMER_CTRL);
  67. /* Enable reset on watchdog */
  68. reg = readl(priv->rstout);
  69. reg |= RSTOUT_ENABLE_BIT;
  70. writel(reg, priv->rstout);
  71. reg = readl(priv->rstout_mask);
  72. reg &= ~RSTOUT_MASK_BIT;
  73. writel(reg, priv->rstout_mask);
  74. return 0;
  75. }
  76. static int orion_wdt_stop(struct udevice *dev)
  77. {
  78. struct orion_wdt_priv *priv = dev_get_priv(dev);
  79. u32 reg;
  80. /* Disable reset on watchdog */
  81. reg = readl(priv->rstout_mask);
  82. reg |= RSTOUT_MASK_BIT;
  83. writel(reg, priv->rstout_mask);
  84. reg = readl(priv->rstout);
  85. reg &= ~RSTOUT_ENABLE_BIT;
  86. writel(reg, priv->rstout);
  87. /* Disable watchdog timer */
  88. reg = readl(priv->reg + TIMER_CTRL);
  89. reg &= ~WDT_ENABLE_BIT;
  90. writel(reg, priv->reg + TIMER_CTRL);
  91. return 0;
  92. }
  93. static inline bool save_reg_from_ofdata(struct udevice *dev, int index,
  94. void __iomem **reg, int *offset)
  95. {
  96. fdt_addr_t addr;
  97. fdt_size_t off;
  98. addr = devfdt_get_addr_size_index(dev, index, &off);
  99. if (addr == FDT_ADDR_T_NONE)
  100. return false;
  101. *reg = (void __iomem *) addr;
  102. if (offset)
  103. *offset = off;
  104. return true;
  105. }
  106. static int orion_wdt_ofdata_to_platdata(struct udevice *dev)
  107. {
  108. struct orion_wdt_priv *priv = dev_get_priv(dev);
  109. if (!save_reg_from_ofdata(dev, 0, &priv->reg,
  110. &priv->wdt_counter_offset))
  111. goto err;
  112. if (!save_reg_from_ofdata(dev, 1, &priv->rstout, NULL))
  113. goto err;
  114. if (!save_reg_from_ofdata(dev, 2, &priv->rstout_mask, NULL))
  115. goto err;
  116. return 0;
  117. err:
  118. debug("%s: Could not determine Orion wdt IO addresses\n", __func__);
  119. return -ENXIO;
  120. }
  121. static int orion_wdt_probe(struct udevice *dev)
  122. {
  123. struct orion_wdt_priv *priv = dev_get_priv(dev);
  124. int ret;
  125. debug("%s: Probing wdt%u\n", __func__, dev->seq);
  126. orion_wdt_stop(dev);
  127. ret = clk_get_by_name(dev, "fixed", &priv->clk);
  128. if (!ret)
  129. priv->clk_rate = clk_get_rate(&priv->clk);
  130. else
  131. priv->clk_rate = 25000000;
  132. return 0;
  133. }
  134. static const struct wdt_ops orion_wdt_ops = {
  135. .start = orion_wdt_start,
  136. .reset = orion_wdt_reset,
  137. .stop = orion_wdt_stop,
  138. };
  139. static const struct udevice_id orion_wdt_ids[] = {
  140. { .compatible = "marvell,armada-380-wdt" },
  141. {}
  142. };
  143. U_BOOT_DRIVER(orion_wdt) = {
  144. .name = "orion_wdt",
  145. .id = UCLASS_WDT,
  146. .of_match = orion_wdt_ids,
  147. .probe = orion_wdt_probe,
  148. .priv_auto_alloc_size = sizeof(struct orion_wdt_priv),
  149. .ofdata_to_platdata = orion_wdt_ofdata_to_platdata,
  150. .ops = &orion_wdt_ops,
  151. };