aspeed_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016 IBM Corporation
  4. *
  5. * Joel Stanley <[email protected]>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/watchdog.h>
  14. static bool nowayout = WATCHDOG_NOWAYOUT;
  15. module_param(nowayout, bool, 0);
  16. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  17. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  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. #define WDT_CLEAR_TIMEOUT_STATUS 0x14
  55. #define WDT_CLEAR_TIMEOUT_AND_BOOT_CODE_SELECTION BIT(0)
  56. /*
  57. * WDT_RESET_WIDTH controls the characteristics of the external pulse (if
  58. * enabled), specifically:
  59. *
  60. * * Pulse duration
  61. * * Drive mode: push-pull vs open-drain
  62. * * Polarity: Active high or active low
  63. *
  64. * Pulse duration configuration is available on both the AST2400 and AST2500,
  65. * though the field changes between SoCs:
  66. *
  67. * AST2400: Bits 7:0
  68. * AST2500: Bits 19:0
  69. *
  70. * This difference is captured in struct aspeed_wdt_config.
  71. *
  72. * The AST2500 exposes the drive mode and polarity options, but not in a
  73. * regular fashion. For read purposes, bit 31 represents active high or low,
  74. * and bit 30 represents push-pull or open-drain. With respect to write, magic
  75. * values need to be written to the top byte to change the state of the drive
  76. * mode and polarity bits. Any other value written to the top byte has no
  77. * effect on the state of the drive mode or polarity bits. However, the pulse
  78. * width value must be preserved (as desired) if written.
  79. */
  80. #define WDT_RESET_WIDTH 0x18
  81. #define WDT_RESET_WIDTH_ACTIVE_HIGH BIT(31)
  82. #define WDT_ACTIVE_HIGH_MAGIC (0xA5 << 24)
  83. #define WDT_ACTIVE_LOW_MAGIC (0x5A << 24)
  84. #define WDT_RESET_WIDTH_PUSH_PULL BIT(30)
  85. #define WDT_PUSH_PULL_MAGIC (0xA8 << 24)
  86. #define WDT_OPEN_DRAIN_MAGIC (0x8A << 24)
  87. #define WDT_RESTART_MAGIC 0x4755
  88. /* 32 bits at 1MHz, in milliseconds */
  89. #define WDT_MAX_TIMEOUT_MS 4294967
  90. #define WDT_DEFAULT_TIMEOUT 30
  91. #define WDT_RATE_1MHZ 1000000
  92. static struct aspeed_wdt *to_aspeed_wdt(struct watchdog_device *wdd)
  93. {
  94. return container_of(wdd, struct aspeed_wdt, wdd);
  95. }
  96. static void aspeed_wdt_enable(struct aspeed_wdt *wdt, int count)
  97. {
  98. wdt->ctrl |= WDT_CTRL_ENABLE;
  99. writel(0, wdt->base + WDT_CTRL);
  100. writel(count, wdt->base + WDT_RELOAD_VALUE);
  101. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  102. writel(wdt->ctrl, wdt->base + WDT_CTRL);
  103. }
  104. static int aspeed_wdt_start(struct watchdog_device *wdd)
  105. {
  106. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  107. aspeed_wdt_enable(wdt, wdd->timeout * WDT_RATE_1MHZ);
  108. return 0;
  109. }
  110. static int aspeed_wdt_stop(struct watchdog_device *wdd)
  111. {
  112. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  113. wdt->ctrl &= ~WDT_CTRL_ENABLE;
  114. writel(wdt->ctrl, wdt->base + WDT_CTRL);
  115. return 0;
  116. }
  117. static int aspeed_wdt_ping(struct watchdog_device *wdd)
  118. {
  119. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  120. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  121. return 0;
  122. }
  123. static int aspeed_wdt_set_timeout(struct watchdog_device *wdd,
  124. unsigned int timeout)
  125. {
  126. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  127. u32 actual;
  128. wdd->timeout = timeout;
  129. actual = min(timeout, wdd->max_hw_heartbeat_ms / 1000);
  130. writel(actual * WDT_RATE_1MHZ, wdt->base + WDT_RELOAD_VALUE);
  131. writel(WDT_RESTART_MAGIC, wdt->base + WDT_RESTART);
  132. return 0;
  133. }
  134. static int aspeed_wdt_restart(struct watchdog_device *wdd,
  135. unsigned long action, void *data)
  136. {
  137. struct aspeed_wdt *wdt = to_aspeed_wdt(wdd);
  138. wdt->ctrl &= ~WDT_CTRL_BOOT_SECONDARY;
  139. aspeed_wdt_enable(wdt, 128 * WDT_RATE_1MHZ / 1000);
  140. mdelay(1000);
  141. return 0;
  142. }
  143. /* access_cs0 shows if cs0 is accessible, hence the reverted bit */
  144. static ssize_t access_cs0_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct aspeed_wdt *wdt = dev_get_drvdata(dev);
  148. u32 status = readl(wdt->base + WDT_TIMEOUT_STATUS);
  149. return sysfs_emit(buf, "%u\n",
  150. !(status & WDT_TIMEOUT_STATUS_BOOT_SECONDARY));
  151. }
  152. static ssize_t access_cs0_store(struct device *dev,
  153. struct device_attribute *attr, const char *buf,
  154. size_t size)
  155. {
  156. struct aspeed_wdt *wdt = dev_get_drvdata(dev);
  157. unsigned long val;
  158. if (kstrtoul(buf, 10, &val))
  159. return -EINVAL;
  160. if (val)
  161. writel(WDT_CLEAR_TIMEOUT_AND_BOOT_CODE_SELECTION,
  162. wdt->base + WDT_CLEAR_TIMEOUT_STATUS);
  163. return size;
  164. }
  165. /*
  166. * This attribute exists only if the system has booted from the alternate
  167. * flash with 'alt-boot' option.
  168. *
  169. * At alternate flash the 'access_cs0' sysfs node provides:
  170. * ast2400: a way to get access to the primary SPI flash chip at CS0
  171. * after booting from the alternate chip at CS1.
  172. * ast2500: a way to restore the normal address mapping from
  173. * (CS0->CS1, CS1->CS0) to (CS0->CS0, CS1->CS1).
  174. *
  175. * Clearing the boot code selection and timeout counter also resets to the
  176. * initial state the chip select line mapping. When the SoC is in normal
  177. * mapping state (i.e. booted from CS0), clearing those bits does nothing for
  178. * both versions of the SoC. For alternate boot mode (booted from CS1 due to
  179. * wdt2 expiration) the behavior differs as described above.
  180. *
  181. * This option can be used with wdt2 (watchdog1) only.
  182. */
  183. static DEVICE_ATTR_RW(access_cs0);
  184. static struct attribute *bswitch_attrs[] = {
  185. &dev_attr_access_cs0.attr,
  186. NULL
  187. };
  188. ATTRIBUTE_GROUPS(bswitch);
  189. static const struct watchdog_ops aspeed_wdt_ops = {
  190. .start = aspeed_wdt_start,
  191. .stop = aspeed_wdt_stop,
  192. .ping = aspeed_wdt_ping,
  193. .set_timeout = aspeed_wdt_set_timeout,
  194. .restart = aspeed_wdt_restart,
  195. .owner = THIS_MODULE,
  196. };
  197. static const struct watchdog_info aspeed_wdt_info = {
  198. .options = WDIOF_KEEPALIVEPING
  199. | WDIOF_MAGICCLOSE
  200. | WDIOF_SETTIMEOUT,
  201. .identity = KBUILD_MODNAME,
  202. };
  203. static int aspeed_wdt_probe(struct platform_device *pdev)
  204. {
  205. struct device *dev = &pdev->dev;
  206. const struct aspeed_wdt_config *config;
  207. const struct of_device_id *ofdid;
  208. struct aspeed_wdt *wdt;
  209. struct device_node *np;
  210. const char *reset_type;
  211. u32 duration;
  212. u32 status;
  213. int ret;
  214. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  215. if (!wdt)
  216. return -ENOMEM;
  217. wdt->base = devm_platform_ioremap_resource(pdev, 0);
  218. if (IS_ERR(wdt->base))
  219. return PTR_ERR(wdt->base);
  220. wdt->wdd.info = &aspeed_wdt_info;
  221. wdt->wdd.ops = &aspeed_wdt_ops;
  222. wdt->wdd.max_hw_heartbeat_ms = WDT_MAX_TIMEOUT_MS;
  223. wdt->wdd.parent = dev;
  224. wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
  225. watchdog_init_timeout(&wdt->wdd, 0, dev);
  226. watchdog_set_nowayout(&wdt->wdd, nowayout);
  227. np = dev->of_node;
  228. ofdid = of_match_node(aspeed_wdt_of_table, np);
  229. if (!ofdid)
  230. return -EINVAL;
  231. config = ofdid->data;
  232. /*
  233. * On clock rates:
  234. * - ast2400 wdt can run at PCLK, or 1MHz
  235. * - ast2500 only runs at 1MHz, hard coding bit 4 to 1
  236. * - ast2600 always runs at 1MHz
  237. *
  238. * Set the ast2400 to run at 1MHz as it simplifies the driver.
  239. */
  240. if (of_device_is_compatible(np, "aspeed,ast2400-wdt"))
  241. wdt->ctrl = WDT_CTRL_1MHZ_CLK;
  242. /*
  243. * Control reset on a per-device basis to ensure the
  244. * host is not affected by a BMC reboot
  245. */
  246. ret = of_property_read_string(np, "aspeed,reset-type", &reset_type);
  247. if (ret) {
  248. wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC | WDT_CTRL_RESET_SYSTEM;
  249. } else {
  250. if (!strcmp(reset_type, "cpu"))
  251. wdt->ctrl |= WDT_CTRL_RESET_MODE_ARM_CPU |
  252. WDT_CTRL_RESET_SYSTEM;
  253. else if (!strcmp(reset_type, "soc"))
  254. wdt->ctrl |= WDT_CTRL_RESET_MODE_SOC |
  255. WDT_CTRL_RESET_SYSTEM;
  256. else if (!strcmp(reset_type, "system"))
  257. wdt->ctrl |= WDT_CTRL_RESET_MODE_FULL_CHIP |
  258. WDT_CTRL_RESET_SYSTEM;
  259. else if (strcmp(reset_type, "none"))
  260. return -EINVAL;
  261. }
  262. if (of_property_read_bool(np, "aspeed,external-signal"))
  263. wdt->ctrl |= WDT_CTRL_WDT_EXT;
  264. if (of_property_read_bool(np, "aspeed,alt-boot"))
  265. wdt->ctrl |= WDT_CTRL_BOOT_SECONDARY;
  266. if (readl(wdt->base + WDT_CTRL) & WDT_CTRL_ENABLE) {
  267. /*
  268. * The watchdog is running, but invoke aspeed_wdt_start() to
  269. * write wdt->ctrl to WDT_CTRL to ensure the watchdog's
  270. * configuration conforms to the driver's expectations.
  271. * Primarily, ensure we're using the 1MHz clock source.
  272. */
  273. aspeed_wdt_start(&wdt->wdd);
  274. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  275. }
  276. if ((of_device_is_compatible(np, "aspeed,ast2500-wdt")) ||
  277. (of_device_is_compatible(np, "aspeed,ast2600-wdt"))) {
  278. u32 reg = readl(wdt->base + WDT_RESET_WIDTH);
  279. reg &= config->ext_pulse_width_mask;
  280. if (of_property_read_bool(np, "aspeed,ext-active-high"))
  281. reg |= WDT_ACTIVE_HIGH_MAGIC;
  282. else
  283. reg |= WDT_ACTIVE_LOW_MAGIC;
  284. writel(reg, wdt->base + WDT_RESET_WIDTH);
  285. reg &= config->ext_pulse_width_mask;
  286. if (of_property_read_bool(np, "aspeed,ext-push-pull"))
  287. reg |= WDT_PUSH_PULL_MAGIC;
  288. else
  289. reg |= WDT_OPEN_DRAIN_MAGIC;
  290. writel(reg, wdt->base + WDT_RESET_WIDTH);
  291. }
  292. if (!of_property_read_u32(np, "aspeed,ext-pulse-duration", &duration)) {
  293. u32 max_duration = config->ext_pulse_width_mask + 1;
  294. if (duration == 0 || duration > max_duration) {
  295. dev_err(dev, "Invalid pulse duration: %uus\n",
  296. duration);
  297. duration = max(1U, min(max_duration, duration));
  298. dev_info(dev, "Pulse duration set to %uus\n",
  299. duration);
  300. }
  301. /*
  302. * The watchdog is always configured with a 1MHz source, so
  303. * there is no need to scale the microsecond value. However we
  304. * need to offset it - from the datasheet:
  305. *
  306. * "This register decides the asserting duration of wdt_ext and
  307. * wdt_rstarm signal. The default value is 0xFF. It means the
  308. * default asserting duration of wdt_ext and wdt_rstarm is
  309. * 256us."
  310. *
  311. * This implies a value of 0 gives a 1us pulse.
  312. */
  313. writel(duration - 1, wdt->base + WDT_RESET_WIDTH);
  314. }
  315. status = readl(wdt->base + WDT_TIMEOUT_STATUS);
  316. if (status & WDT_TIMEOUT_STATUS_BOOT_SECONDARY) {
  317. wdt->wdd.bootstatus = WDIOF_CARDRESET;
  318. if (of_device_is_compatible(np, "aspeed,ast2400-wdt") ||
  319. of_device_is_compatible(np, "aspeed,ast2500-wdt"))
  320. wdt->wdd.groups = bswitch_groups;
  321. }
  322. dev_set_drvdata(dev, wdt);
  323. return devm_watchdog_register_device(dev, &wdt->wdd);
  324. }
  325. static struct platform_driver aspeed_watchdog_driver = {
  326. .probe = aspeed_wdt_probe,
  327. .driver = {
  328. .name = KBUILD_MODNAME,
  329. .of_match_table = of_match_ptr(aspeed_wdt_of_table),
  330. },
  331. };
  332. static int __init aspeed_wdt_init(void)
  333. {
  334. return platform_driver_register(&aspeed_watchdog_driver);
  335. }
  336. arch_initcall(aspeed_wdt_init);
  337. static void __exit aspeed_wdt_exit(void)
  338. {
  339. platform_driver_unregister(&aspeed_watchdog_driver);
  340. }
  341. module_exit(aspeed_wdt_exit);
  342. MODULE_DESCRIPTION("Aspeed Watchdog Driver");
  343. MODULE_LICENSE("GPL");