apple_wdt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. u32 actual;
  81. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  82. actual = min(s, wdd->max_hw_heartbeat_ms / 1000);
  83. writel_relaxed(wdt->clk_rate * actual, wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  84. wdd->timeout = s;
  85. return 0;
  86. }
  87. static unsigned int apple_wdt_get_timeleft(struct watchdog_device *wdd)
  88. {
  89. struct apple_wdt *wdt = to_apple_wdt(wdd);
  90. u32 cur_time, reset_time;
  91. cur_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  92. reset_time = readl_relaxed(wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  93. return (reset_time - cur_time) / wdt->clk_rate;
  94. }
  95. static int apple_wdt_restart(struct watchdog_device *wdd, unsigned long mode,
  96. void *cmd)
  97. {
  98. struct apple_wdt *wdt = to_apple_wdt(wdd);
  99. writel_relaxed(APPLE_WDT_CTRL_RESET_EN, wdt->regs + APPLE_WDT_WD1_CTRL);
  100. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_BITE_TIME);
  101. writel_relaxed(0, wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  102. /*
  103. * Flush writes and then wait for the SoC to reset. Even though the
  104. * reset is queued almost immediately experiments have shown that it
  105. * can take up to ~120-125ms until the SoC is actually reset. Just
  106. * wait 150ms here to be safe.
  107. */
  108. (void)readl(wdt->regs + APPLE_WDT_WD1_CUR_TIME);
  109. mdelay(150);
  110. return 0;
  111. }
  112. static struct watchdog_ops apple_wdt_ops = {
  113. .owner = THIS_MODULE,
  114. .start = apple_wdt_start,
  115. .stop = apple_wdt_stop,
  116. .ping = apple_wdt_ping,
  117. .set_timeout = apple_wdt_set_timeout,
  118. .get_timeleft = apple_wdt_get_timeleft,
  119. .restart = apple_wdt_restart,
  120. };
  121. static struct watchdog_info apple_wdt_info = {
  122. .identity = "Apple SoC Watchdog",
  123. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT,
  124. };
  125. static int apple_wdt_probe(struct platform_device *pdev)
  126. {
  127. struct device *dev = &pdev->dev;
  128. struct apple_wdt *wdt;
  129. struct clk *clk;
  130. u32 wdt_ctrl;
  131. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  132. if (!wdt)
  133. return -ENOMEM;
  134. wdt->regs = devm_platform_ioremap_resource(pdev, 0);
  135. if (IS_ERR(wdt->regs))
  136. return PTR_ERR(wdt->regs);
  137. clk = devm_clk_get_enabled(dev, NULL);
  138. if (IS_ERR(clk))
  139. return PTR_ERR(clk);
  140. wdt->clk_rate = clk_get_rate(clk);
  141. if (!wdt->clk_rate)
  142. return -EINVAL;
  143. platform_set_drvdata(pdev, wdt);
  144. wdt->wdd.ops = &apple_wdt_ops;
  145. wdt->wdd.info = &apple_wdt_info;
  146. wdt->wdd.max_hw_heartbeat_ms = U32_MAX / wdt->clk_rate * 1000;
  147. wdt->wdd.timeout = APPLE_WDT_TIMEOUT_DEFAULT;
  148. wdt_ctrl = readl_relaxed(wdt->regs + APPLE_WDT_WD1_CTRL);
  149. if (wdt_ctrl & APPLE_WDT_CTRL_RESET_EN)
  150. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  151. watchdog_init_timeout(&wdt->wdd, 0, dev);
  152. apple_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
  153. watchdog_stop_on_unregister(&wdt->wdd);
  154. watchdog_set_restart_priority(&wdt->wdd, 128);
  155. return devm_watchdog_register_device(dev, &wdt->wdd);
  156. }
  157. static int apple_wdt_resume(struct device *dev)
  158. {
  159. struct apple_wdt *wdt = dev_get_drvdata(dev);
  160. if (watchdog_active(&wdt->wdd) || watchdog_hw_running(&wdt->wdd))
  161. apple_wdt_start(&wdt->wdd);
  162. return 0;
  163. }
  164. static int apple_wdt_suspend(struct device *dev)
  165. {
  166. struct apple_wdt *wdt = dev_get_drvdata(dev);
  167. if (watchdog_active(&wdt->wdd) || watchdog_hw_running(&wdt->wdd))
  168. apple_wdt_stop(&wdt->wdd);
  169. return 0;
  170. }
  171. static DEFINE_SIMPLE_DEV_PM_OPS(apple_wdt_pm_ops, apple_wdt_suspend, apple_wdt_resume);
  172. static const struct of_device_id apple_wdt_of_match[] = {
  173. { .compatible = "apple,wdt" },
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(of, apple_wdt_of_match);
  177. static struct platform_driver apple_wdt_driver = {
  178. .driver = {
  179. .name = "apple-watchdog",
  180. .of_match_table = apple_wdt_of_match,
  181. .pm = pm_sleep_ptr(&apple_wdt_pm_ops),
  182. },
  183. .probe = apple_wdt_probe,
  184. };
  185. module_platform_driver(apple_wdt_driver);
  186. MODULE_DESCRIPTION("Apple SoC watchdog driver");
  187. MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
  188. MODULE_LICENSE("Dual MIT/GPL");