aspeed_wdt.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright 2016 IBM Corporation
  3. *
  4. * Joel Stanley <joel@jms.id.au>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/watchdog.h>
  18. struct aspeed_wdt {
  19. struct watchdog_device wdd;
  20. void __iomem *base;
  21. u32 ctrl;
  22. };
  23. struct aspeed_wdt_config {
  24. u32 ext_pulse_width_mask;
  25. };
  26. static const struct aspeed_wdt_config ast2400_config = {
  27. .ext_pulse_width_mask = 0xff,
  28. };
  29. static const struct aspeed_wdt_config ast2500_config = {
  30. .ext_pulse_width_mask = 0xfffff,
  31. };
  32. static const struct of_device_id aspeed_wdt_of_table[] = {
  33. { .compatible = "aspeed,ast2400-wdt", .data = &ast2400_config },
  34. { .compatible = "aspeed,ast2500-wdt", .data = &ast2500_config },
  35. { .compatible = "aspeed,ast2600-wdt", .data = &ast2500_config },
  36. { },
  37. };
  38. MODULE_DEVICE_TABLE(of, aspeed_wdt_of_table);
  39. #define WDT_STATUS 0x00
  40. #define WDT_RELOAD_VALUE 0x04
  41. #define WDT_RESTART 0x08
  42. #define WDT_CTRL 0x0C
  43. #define WDT_CTRL_BOOT_SECONDARY BIT(7)
  44. #define WDT_CTRL_RESET_MODE_SOC (0x00 << 5)
  45. #define WDT_CTRL_RESET_MODE_FULL_CHIP (0x01 << 5)
  46. #define WDT_CTRL_RESET_MODE_ARM_CPU (0x10 << 5)
  47. #define WDT_CTRL_1MHZ_CLK BIT(4)
  48. #define WDT_CTRL_WDT_EXT BIT(3)
  49. #define WDT_CTRL_WDT_INTR BIT(2)
  50. #define WDT_CTRL_RESET_SYSTEM BIT(1)
  51. #define WDT_CTRL_ENABLE BIT(0)
  52. #define WDT_TIMEOUT_STATUS 0x10
  53. #define WDT_TIMEOUT_STATUS_BOOT_SECONDARY BIT(1)
  54. /*
  55. * WDT_RESET_WIDTH controls the characteristics of the external pulse (if
  56. * enabled), specifically:
  57. *
  58. * * Pulse duration
  59. * * Drive mode: push-pull vs open-drain
  60. * * Polarity: Active high or active low
  61. *
  62. * Pulse duration configuration is available on both the AST2400 and AST2500,
  63. * though the field changes between SoCs:
  64. *
  65. * AST2400: Bits 7:0
  66. * AST2500: Bits 19:0
  67. *
  68. * This difference is captured in struct aspeed_wdt_config.
  69. *
  70. * The AST2500 exposes the drive mode and polarity options, but not in a
  71. * regular fashion. For read purposes, bit 31 represents active high or low,
  72. * and bit 30 represents push-pull or open-drain. With respect to write, magic
  73. * values need to be written to the top byte to change the state of the drive
  74. * mode and polarity bits. Any other value written to the top byte has no
  75. * effect on the state of the drive mode or polarity bits. However, the pulse
  76. * width value must be preserved (as desired) if written.
  77. */
  78. #define WDT_RESET_WIDTH 0x18
  79. #define WDT_RESET_WIDTH_ACTIVE_HIGH BIT(31)
  80. #define WDT_ACTIVE_HIGH_MAGIC (0xA5 << 24)
  81. #define WDT_ACTIVE_LOW_MAGIC (0x5A << 24)
  82. #define WDT_RESET_WIDTH_PUSH_PULL BIT(30)
  83. #define WDT_PUSH_PULL_MAGIC (0xA8 << 24)
  84. #define WDT_OPEN_DRAIN_MAGIC (0x8A << 24)
  85. #define WDT_RESTART_MAGIC 0x4755
  86. /* 32 bits at 1MHz, in milliseconds */
  87. #define WDT_MAX_TIMEOUT_MS 4294967
  88. #define WDT_DEFAULT_TIMEOUT 30
  89. #define WDT_RATE_1MHZ 1000000
  90. static struct aspeed_wdt *to_aspeed_wdt(struct watchdog_device *wdd)
  91. {
  92. return container_of(wdd, struct aspeed_wdt, wdd);
  93. }
  94. static void aspeed_wdt_enable(struct aspeed_wdt *wdt, int count)
  95. {
  96. wdt->ctrl |= WDT_CTRL_ENABLE;
  97. writel(0, wdt->base + WDT_CTRL);
  98. writel(count, wdt->base + WDT_RELOAD_VALUE);
  99. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  100. writel(wdt->ctrl, wdt->base + WDT_CTRL);
  101. }
  102. static int aspeed_wdt_start(struct watchdog_device *wdd)
  103. {
  104. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  105. aspeed_wdt_enable(wdt, wdd->timeout * WDT_RATE_1MHZ);
  106. return 0;
  107. }
  108. static int aspeed_wdt_stop(struct watchdog_device *wdd)
  109. {
  110. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  111. wdt->ctrl &= ~WDT_CTRL_ENABLE;
  112. writel(wdt->ctrl, wdt->base + WDT_CTRL);
  113. return 0;
  114. }
  115. static int aspeed_wdt_ping(struct watchdog_device *wdd)
  116. {
  117. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  118. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  119. return 0;
  120. }
  121. static int aspeed_wdt_set_timeout(struct watchdog_device *wdd,
  122. unsigned int timeout)
  123. {
  124. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  125. u32 actual;
  126. wdd->timeout = timeout;
  127. actual = min(timeout, wdd->max_hw_heartbeat_ms * 1000);
  128. writel(actual * WDT_RATE_1MHZ, wdt->base + WDT_RELOAD_VALUE);
  129. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  130. return 0;
  131. }
  132. static int aspeed_wdt_restart(struct watchdog_device *wdd,
  133. unsigned long action, void *data)
  134. {
  135. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  136. wdt->ctrl &= ~WDT_CTRL_BOOT_SECONDARY;
  137. aspeed_wdt_enable(wdt, 128 * WDT_RATE_1MHZ / 1000);
  138. mdelay(1000);
  139. return 0;
  140. }
  141. static const struct watchdog_ops aspeed_wdt_ops = {
  142. .start = aspeed_wdt_start,
  143. .stop = aspeed_wdt_stop,
  144. .ping = aspeed_wdt_ping,
  145. .set_timeout = aspeed_wdt_set_timeout,
  146. .restart = aspeed_wdt_restart,
  147. .owner = THIS_MODULE,
  148. };
  149. static const struct watchdog_info aspeed_wdt_info = {
  150. .options = WDIOF_KEEPALIVEPING
  151. | WDIOF_MAGICCLOSE
  152. | WDIOF_SETTIMEOUT,
  153. .identity = KBUILD_MODNAME,
  154. };
  155. static int aspeed_wdt_probe(struct platform_device *pdev)
  156. {
  157. const struct aspeed_wdt_config *config;
  158. const struct of_device_id *ofdid;
  159. struct aspeed_wdt *wdt;
  160. struct resource *res;
  161. struct device_node *np;
  162. const char *reset_type;
  163. u32 duration;
  164. u32 status;
  165. int ret;
  166. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  167. if (!wdt)
  168. return -ENOMEM;
  169. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  170. wdt->base = devm_ioremap_resource(&pdev->dev, res);
  171. if (IS_ERR(wdt->base))
  172. return PTR_ERR(wdt->base);
  173. wdt->wdd.info = &aspeed_wdt_info;
  174. wdt->wdd.ops = &aspeed_wdt_ops;
  175. wdt->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
  176. wdt->wdd.parent = &pdev->dev;
  177. wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
  178. watchdog_init_timeout(&wdt->wdd, 0, &pdev->dev);
  179. np = pdev->dev.of_node;
  180. ofdid = of_match_node(aspeed_wdt_of_table, np);
  181. if (!ofdid)
  182. return -EINVAL;
  183. config = ofdid->data;
  184. /*
  185. * On clock rates:
  186. * - ast2400 wdt can run at PCLK, or 1MHz
  187. * - ast2500 only runs at 1MHz, hard coding bit 4 to 1
  188. * - ast2600 always runs at 1MHz
  189. *
  190. * Set the ast2400 to run at 1MHz as it simplifies the driver.
  191. */
  192. if (of_device_is_compatible(np, "aspeed,ast2400-wdt"))
  193. wdt->ctrl = WDT_CTRL_1MHZ_CLK;
  194. /*
  195. * Control reset on a per-device basis to ensure the
  196. * host is not affected by a BMC reboot
  197. */
  198. ret = of_property_read_string(np, "aspeed,reset-type", &reset_type);
  199. if (ret) {
  200. wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC | WDT_CTRL_RESET_SYSTEM;
  201. } else {
  202. if (!strcmp(reset_type, "cpu"))
  203. wdt->ctrl |= WDT_CTRL_RESET_MODE_ARM_CPU |
  204. WDT_CTRL_RESET_SYSTEM;
  205. else if (!strcmp(reset_type, "soc"))
  206. wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC |
  207. WDT_CTRL_RESET_SYSTEM;
  208. else if (!strcmp(reset_type, "system"))
  209. wdt->ctrl |= WDT_CTRL_RESET_MODE_FULL_CHIP |
  210. WDT_CTRL_RESET_SYSTEM;
  211. else if (strcmp(reset_type, "none"))
  212. return -EINVAL;
  213. }
  214. if (of_property_read_bool(np, "aspeed,external-signal"))
  215. wdt->ctrl |= WDT_CTRL_WDT_EXT;
  216. if (of_property_read_bool(np, "aspeed,alt-boot"))
  217. wdt->ctrl |= WDT_CTRL_BOOT_SECONDARY;
  218. if (readl(wdt->base + WDT_CTRL) & WDT_CTRL_ENABLE) {
  219. /*
  220. * The watchdog is running, but invoke aspeed_wdt_start() to
  221. * write wdt->ctrl to WDT_CTRL to ensure the watchdog's
  222. * configuration conforms to the driver's expectations.
  223. * Primarily, ensure we're using the 1MHz clock source.
  224. */
  225. aspeed_wdt_start(&wdt->wdd);
  226. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  227. }
  228. if ((of_device_is_compatible(np, "aspeed,ast2500-wdt")) ||
  229. (of_device_is_compatible(np, "aspeed,ast2600-wdt"))) {
  230. u32 reg = readl(wdt->base + WDT_RESET_WIDTH);
  231. reg &= config->ext_pulse_width_mask;
  232. if (of_property_read_bool(np, "aspeed,ext-push-pull"))
  233. reg |= WDT_PUSH_PULL_MAGIC;
  234. else
  235. reg |= WDT_OPEN_DRAIN_MAGIC;
  236. writel(reg, wdt->base + WDT_RESET_WIDTH);
  237. reg &= config->ext_pulse_width_mask;
  238. if (of_property_read_bool(np, "aspeed,ext-active-high"))
  239. reg |= WDT_ACTIVE_HIGH_MAGIC;
  240. else
  241. reg |= WDT_ACTIVE_LOW_MAGIC;
  242. writel(reg, wdt->base + WDT_RESET_WIDTH);
  243. }
  244. if (!of_property_read_u32(np, "aspeed,ext-pulse-duration", &duration)) {
  245. u32 max_duration = config->ext_pulse_width_mask + 1;
  246. if (duration == 0 || duration > max_duration) {
  247. dev_err(&pdev->dev, "Invalid pulse duration: %uus\n",
  248. duration);
  249. duration = max(1U, min(max_duration, duration));
  250. dev_info(&pdev->dev, "Pulse duration set to %uus\n",
  251. duration);
  252. }
  253. /*
  254. * The watchdog is always configured with a 1MHz source, so
  255. * there is no need to scale the microsecond value. However we
  256. * need to offset it - from the datasheet:
  257. *
  258. * "This register decides the asserting duration of wdt_ext and
  259. * wdt_rstarm signal. The default value is 0xFF. It means the
  260. * default asserting duration of wdt_ext and wdt_rstarm is
  261. * 256us."
  262. *
  263. * This implies a value of 0 gives a 1us pulse.
  264. */
  265. writel(duration - 1, wdt->base + WDT_RESET_WIDTH);
  266. }
  267. status = readl(wdt->base + WDT_TIMEOUT_STATUS);
  268. if (status & WDT_TIMEOUT_STATUS_BOOT_SECONDARY)
  269. wdt->wdd.bootstatus = WDIOF_CARDRESET;
  270. ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdd);
  271. if (ret) {
  272. dev_err(&pdev->dev, "failed to register\n");
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static struct platform_driver aspeed_watchdog_driver = {
  278. .probe = aspeed_wdt_probe,
  279. .driver = {
  280. .name = KBUILD_MODNAME,
  281. .of_match_table = of_match_ptr(aspeed_wdt_of_table),
  282. },
  283. };
  284. static int __init aspeed_wdt_init(void)
  285. {
  286. return platform_driver_register(&aspeed_watchdog_driver);
  287. }
  288. arch_initcall(aspeed_wdt_init);
  289. static void __exit aspeed_wdt_exit(void)
  290. {
  291. platform_driver_unregister(&aspeed_watchdog_driver);
  292. }
  293. module_exit(aspeed_wdt_exit);
  294. MODULE_DESCRIPTION("Aspeed Watchdog Driver");
  295. MODULE_LICENSE("GPL");