apple_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Apple SoC Watchdog driver
  4. *
  5. * Copyright (C) The Asahi Linux Contributors
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/limits.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/watchdog.h>
  17. /*
  18. * Apple Watchdog MMIO registers
  19. *
  20. * This HW block has three separate watchdogs. WD0 resets the machine
  21. * to recovery mode and is not very useful for us. WD1 and WD2 trigger a normal
  22. * machine reset. WD0 additionally supports a configurable interrupt.
  23. * This information can be used to implement pretimeout support at a later time.
  24. *
  25. * APPLE_WDT_WDx_CUR_TIME is a simple counter incremented for each tick of the
  26. * reference clock. It can also be overwritten to any value.
  27. * Whenever APPLE_WDT_CTRL_RESET_EN is set in APPLE_WDT_WDx_CTRL and
  28. * APPLE_WDT_WDx_CUR_TIME >= APPLE_WDT_WDx_BITE_TIME the entire machine is
  29. * reset.
  30. * Whenever APPLE_WDT_CTRL_IRQ_EN is set and APPLE_WDTx_WD1_CUR_TIME >=
  31. * APPLE_WDTx_WD1_BARK_TIME an interrupt is triggered and
  32. * APPLE_WDT_CTRL_IRQ_STATUS is set. The interrupt can be cleared by writing
  33. * 1 to APPLE_WDT_CTRL_IRQ_STATUS.
  34. */
  35. #define APPLE_WDT_WD0_CUR_TIME 0x00
  36. #define APPLE_WDT_WD0_BITE_TIME 0x04
  37. #define APPLE_WDT_WD0_BARK_TIME 0x08
  38. #define APPLE_WDT_WD0_CTRL 0x0c
  39. #define APPLE_WDT_WD1_CUR_TIME 0x10
  40. #define APPLE_WDT_WD1_BITE_TIME 0x14
  41. #define APPLE_WDT_WD1_CTRL 0x1c
  42. #define APPLE_WDT_WD2_CUR_TIME 0x20
  43. #define APPLE_WDT_WD2_BITE_TIME 0x24
  44. #define APPLE_WDT_WD2_CTRL 0x2c
  45. #define APPLE_WDT_CTRL_IRQ_EN BIT(0)
  46. #define APPLE_WDT_CTRL_IRQ_STATUS BIT(1)
  47. #define APPLE_WDT_CTRL_RESET_EN BIT(2)
  48. #define APPLE_WDT_TIMEOUT_DEFAULT 30
  49. struct apple_wdt {
  50. struct watchdog_device wdd;
  51. void __iomem *regs;
  52. unsigned long clk_rate;
  53. };
  54. static struct apple_wdt *to_apple_wdt(struct watchdog_device *wdd)
  55. {
  56. return container_of(wdd, struct apple_wdt, wdd);
  57. }
  58. static int apple_wdt_start(struct watchdog_device *wdd)
  59. {
  60. struct apple_wdt *wdt = to_apple_wdt(wdd);
  61. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  62. writel_relaxed(APPLE_WDT_CTRL_RESET_EN, wdt->regs + APPLE_WDT_WD1_CTRL);
  63. return 0;
  64. }
  65. static int apple_wdt_stop(struct watchdog_device *wdd)
  66. {
  67. struct apple_wdt *wdt = to_apple_wdt(wdd);
  68. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CTRL);
  69. return 0;
  70. }
  71. static int apple_wdt_ping(struct watchdog_device *wdd)
  72. {
  73. struct apple_wdt *wdt = to_apple_wdt(wdd);
  74. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  75. return 0;
  76. }
  77. static int apple_wdt_set_timeout(struct watchdog_device *wdd, unsigned int s)
  78. {
  79. struct apple_wdt *wdt = to_apple_wdt(wdd);
  80. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  81. writel_relaxed(wdt->clk_rate * s, wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  82. wdd->timeout = s;
  83. return 0;
  84. }
  85. static unsigned int apple_wdt_get_timeleft(struct watchdog_device *wdd)
  86. {
  87. struct apple_wdt *wdt = to_apple_wdt(wdd);
  88. u32 cur_time, reset_time;
  89. cur_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  90. reset_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  91. return (reset_time - cur_time) / wdt->clk_rate;
  92. }
  93. static int apple_wdt_restart(struct watchdog_device *wdd, unsigned long mode,
  94. void *cmd)
  95. {
  96. struct apple_wdt *wdt = to_apple_wdt(wdd);
  97. writel_relaxed(APPLE_WDT_CTRL_RESET_EN, wdt->regs + APPLE_WDT_WD1_CTRL);
  98. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  99. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  100. /*
  101. * Flush writes and then wait for the SoC to reset. Even though the
  102. * reset is queued almost immediately experiments have shown that it
  103. * can take up to ~20-25ms until the SoC is actually reset. Just wait
  104. * 50ms here to be safe.
  105. */
  106. (void)readl_relaxed(wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  107. mdelay(50);
  108. return 0;
  109. }
  110. static void apple_wdt_clk_disable_unprepare(void *data)
  111. {
  112. clk_disable_unprepare(data);
  113. }
  114. static struct watchdog_ops apple_wdt_ops = {
  115. .owner = THIS_MODULE,
  116. .start = apple_wdt_start,
  117. .stop = apple_wdt_stop,
  118. .ping = apple_wdt_ping,
  119. .set_timeout = apple_wdt_set_timeout,
  120. .get_timeleft = apple_wdt_get_timeleft,
  121. .restart = apple_wdt_restart,
  122. };
  123. static struct watchdog_info apple_wdt_info = {
  124. .identity = "Apple SoC Watchdog",
  125. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  126. };
  127. static int apple_wdt_probe(struct platform_device *pdev)
  128. {
  129. struct device *dev = &pdev->dev;
  130. struct apple_wdt *wdt;
  131. struct clk *clk;
  132. u32 wdt_ctrl;
  133. int ret;
  134. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  135. if (!wdt)
  136. return -ENOMEM;
  137. wdt->regs = devm_platform_ioremap_resource(pdev, 0);
  138. if (IS_ERR(wdt->regs))
  139. return PTR_ERR(wdt->regs);
  140. clk = devm_clk_get(dev, NULL);
  141. if (IS_ERR(clk))
  142. return PTR_ERR(clk);
  143. ret = clk_prepare_enable(clk);
  144. if (ret)
  145. return ret;
  146. ret = devm_add_action_or_reset(dev, apple_wdt_clk_disable_unprepare,
  147. clk);
  148. if (ret)
  149. return ret;
  150. wdt->clk_rate = clk_get_rate(clk);
  151. if (!wdt->clk_rate)
  152. return -EINVAL;
  153. wdt->wdd.ops = &apple_wdt_ops;
  154. wdt->wdd.info = &apple_wdt_info;
  155. wdt->wdd.max_timeout = U32_MAX / wdt->clk_rate;
  156. wdt->wdd.timeout = APPLE_WDT_TIMEOUT_DEFAULT;
  157. wdt_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CTRL);
  158. if (wdt_ctrl & APPLE_WDT_CTRL_RESET_EN)
  159. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  160. watchdog_init_timeout(&wdt->wdd, 0, dev);
  161. apple_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
  162. watchdog_stop_on_unregister(&wdt->wdd);
  163. watchdog_set_restart_priority(&wdt->wdd, 128);
  164. return devm_watchdog_register_device(dev, &wdt->wdd);
  165. }
  166. static const struct of_device_id apple_wdt_of_match[] = {
  167. { .compatible = "apple,wdt" },
  168. {},
  169. };
  170. MODULE_DEVICE_TABLE(of, apple_wdt_of_match);
  171. static struct platform_driver apple_wdt_driver = {
  172. .driver = {
  173. .name = "apple-watchdog",
  174. .of_match_table = apple_wdt_of_match,
  175. },
  176. .probe = apple_wdt_probe,
  177. };
  178. module_platform_driver(apple_wdt_driver);
  179. MODULE_DESCRIPTION("Apple SoC watchdog driver");
  180. MODULE_AUTHOR("Sven Peter <[email protected]>");
  181. MODULE_LICENSE("Dual MIT/GPL");