sunxi_wdt.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * sunxi Watchdog Driver
  4. *
  5. * Copyright (c) 2013 Carlo Caione
  6. * 2012 Henrik Nordstrom
  7. *
  8. * Based on xen_wdt.c
  9. * (c) Copyright 2010 Novell, Inc.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/types.h>
  22. #include <linux/watchdog.h>
  23. #define WDT_MAX_TIMEOUT 16
  24. #define WDT_MIN_TIMEOUT 1
  25. #define WDT_TIMEOUT_MASK 0x0F
  26. #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
  27. #define WDT_MODE_EN (1 << 0)
  28. #define DRV_NAME "sunxi-wdt"
  29. #define DRV_VERSION "1.0"
  30. static bool nowayout = WATCHDOG_NOWAYOUT;
  31. static unsigned int timeout;
  32. /*
  33. * This structure stores the register offsets for different variants
  34. * of Allwinner's watchdog hardware.
  35. */
  36. struct sunxi_wdt_reg {
  37. u8 wdt_ctrl;
  38. u8 wdt_cfg;
  39. u8 wdt_mode;
  40. u8 wdt_timeout_shift;
  41. u8 wdt_reset_mask;
  42. u8 wdt_reset_val;
  43. u32 wdt_key_val;
  44. };
  45. struct sunxi_wdt_dev {
  46. struct watchdog_device wdt_dev;
  47. void __iomem *wdt_base;
  48. const struct sunxi_wdt_reg *wdt_regs;
  49. };
  50. /*
  51. * wdt_timeout_map maps the watchdog timer interval value in seconds to
  52. * the value of the register WDT_MODE at bits .wdt_timeout_shift ~ +3
  53. *
  54. * [timeout seconds] = register value
  55. *
  56. */
  57. static const int wdt_timeout_map[] = {
  58. [1] = 0x1, /* 1s */
  59. [2] = 0x2, /* 2s */
  60. [3] = 0x3, /* 3s */
  61. [4] = 0x4, /* 4s */
  62. [5] = 0x5, /* 5s */
  63. [6] = 0x6, /* 6s */
  64. [8] = 0x7, /* 8s */
  65. [10] = 0x8, /* 10s */
  66. [12] = 0x9, /* 12s */
  67. [14] = 0xA, /* 14s */
  68. [16] = 0xB, /* 16s */
  69. };
  70. static int sunxi_wdt_restart(struct watchdog_device *wdt_dev,
  71. unsigned long action, void *data)
  72. {
  73. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  74. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  75. const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
  76. u32 val;
  77. /* Set system reset function */
  78. val = readl(wdt_base + regs->wdt_cfg);
  79. val &= ~(regs->wdt_reset_mask);
  80. val |= regs->wdt_reset_val;
  81. val |= regs->wdt_key_val;
  82. writel(val, wdt_base + regs->wdt_cfg);
  83. /* Set lowest timeout and enable watchdog */
  84. val = readl(wdt_base + regs->wdt_mode);
  85. val &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
  86. val |= WDT_MODE_EN;
  87. val |= regs->wdt_key_val;
  88. writel(val, wdt_base + regs->wdt_mode);
  89. /*
  90. * Restart the watchdog. The default (and lowest) interval
  91. * value for the watchdog is 0.5s.
  92. */
  93. writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
  94. while (1) {
  95. mdelay(5);
  96. val = readl(wdt_base + regs->wdt_mode);
  97. val |= WDT_MODE_EN;
  98. val |= regs->wdt_key_val;
  99. writel(val, wdt_base + regs->wdt_mode);
  100. }
  101. return 0;
  102. }
  103. static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
  104. {
  105. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  106. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  107. const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
  108. writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
  109. return 0;
  110. }
  111. static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
  112. unsigned int timeout)
  113. {
  114. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  115. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  116. const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
  117. u32 reg;
  118. if (wdt_timeout_map[timeout] == 0)
  119. timeout++;
  120. sunxi_wdt->wdt_dev.timeout = timeout;
  121. reg = readl(wdt_base + regs->wdt_mode);
  122. reg &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
  123. reg |= wdt_timeout_map[timeout] << regs->wdt_timeout_shift;
  124. reg |= regs->wdt_key_val;
  125. writel(reg, wdt_base + regs->wdt_mode);
  126. sunxi_wdt_ping(wdt_dev);
  127. return 0;
  128. }
  129. static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
  130. {
  131. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  132. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  133. const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
  134. writel(regs->wdt_key_val, wdt_base + regs->wdt_mode);
  135. return 0;
  136. }
  137. static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
  138. {
  139. u32 reg;
  140. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  141. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  142. const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
  143. int ret;
  144. ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
  145. sunxi_wdt->wdt_dev.timeout);
  146. if (ret < 0)
  147. return ret;
  148. /* Set system reset function */
  149. reg = readl(wdt_base + regs->wdt_cfg);
  150. reg &= ~(regs->wdt_reset_mask);
  151. reg |= regs->wdt_reset_val;
  152. reg |= regs->wdt_key_val;
  153. writel(reg, wdt_base + regs->wdt_cfg);
  154. /* Enable watchdog */
  155. reg = readl(wdt_base + regs->wdt_mode);
  156. reg |= WDT_MODE_EN;
  157. reg |= regs->wdt_key_val;
  158. writel(reg, wdt_base + regs->wdt_mode);
  159. return 0;
  160. }
  161. static const struct watchdog_info sunxi_wdt_info = {
  162. .identity = DRV_NAME,
  163. .options = WDIOF_SETTIMEOUT |
  164. WDIOF_KEEPALIVEPING |
  165. WDIOF_MAGICCLOSE,
  166. };
  167. static const struct watchdog_ops sunxi_wdt_ops = {
  168. .owner = THIS_MODULE,
  169. .start = sunxi_wdt_start,
  170. .stop = sunxi_wdt_stop,
  171. .ping = sunxi_wdt_ping,
  172. .set_timeout = sunxi_wdt_set_timeout,
  173. .restart = sunxi_wdt_restart,
  174. };
  175. static const struct sunxi_wdt_reg sun4i_wdt_reg = {
  176. .wdt_ctrl = 0x00,
  177. .wdt_cfg = 0x04,
  178. .wdt_mode = 0x04,
  179. .wdt_timeout_shift = 3,
  180. .wdt_reset_mask = 0x02,
  181. .wdt_reset_val = 0x02,
  182. };
  183. static const struct sunxi_wdt_reg sun6i_wdt_reg = {
  184. .wdt_ctrl = 0x10,
  185. .wdt_cfg = 0x14,
  186. .wdt_mode = 0x18,
  187. .wdt_timeout_shift = 4,
  188. .wdt_reset_mask = 0x03,
  189. .wdt_reset_val = 0x01,
  190. };
  191. static const struct sunxi_wdt_reg sun20i_wdt_reg = {
  192. .wdt_ctrl = 0x10,
  193. .wdt_cfg = 0x14,
  194. .wdt_mode = 0x18,
  195. .wdt_timeout_shift = 4,
  196. .wdt_reset_mask = 0x03,
  197. .wdt_reset_val = 0x01,
  198. .wdt_key_val = 0x16aa0000,
  199. };
  200. static const struct of_device_id sunxi_wdt_dt_ids[] = {
  201. { .compatible = "allwinner,sun4i-a10-wdt", .data = &sun4i_wdt_reg },
  202. { .compatible = "allwinner,sun6i-a31-wdt", .data = &sun6i_wdt_reg },
  203. { .compatible = "allwinner,sun20i-d1-wdt", .data = &sun20i_wdt_reg },
  204. { /* sentinel */ }
  205. };
  206. MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
  207. static int sunxi_wdt_probe(struct platform_device *pdev)
  208. {
  209. struct device *dev = &pdev->dev;
  210. struct sunxi_wdt_dev *sunxi_wdt;
  211. int err;
  212. sunxi_wdt = devm_kzalloc(dev, sizeof(*sunxi_wdt), GFP_KERNEL);
  213. if (!sunxi_wdt)
  214. return -ENOMEM;
  215. sunxi_wdt->wdt_regs = of_device_get_match_data(dev);
  216. if (!sunxi_wdt->wdt_regs)
  217. return -ENODEV;
  218. sunxi_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
  219. if (IS_ERR(sunxi_wdt->wdt_base))
  220. return PTR_ERR(sunxi_wdt->wdt_base);
  221. sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
  222. sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
  223. sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
  224. sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
  225. sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
  226. sunxi_wdt->wdt_dev.parent = dev;
  227. watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, dev);
  228. watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
  229. watchdog_set_restart_priority(&sunxi_wdt->wdt_dev, 128);
  230. watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
  231. sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
  232. watchdog_stop_on_reboot(&sunxi_wdt->wdt_dev);
  233. err = devm_watchdog_register_device(dev, &sunxi_wdt->wdt_dev);
  234. if (unlikely(err))
  235. return err;
  236. dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  237. sunxi_wdt->wdt_dev.timeout, nowayout);
  238. return 0;
  239. }
  240. static struct platform_driver sunxi_wdt_driver = {
  241. .probe = sunxi_wdt_probe,
  242. .driver = {
  243. .name = DRV_NAME,
  244. .of_match_table = sunxi_wdt_dt_ids,
  245. },
  246. };
  247. module_platform_driver(sunxi_wdt_driver);
  248. module_param(timeout, uint, 0);
  249. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  250. module_param(nowayout, bool, 0);
  251. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  252. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  253. MODULE_LICENSE("GPL");
  254. MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
  255. MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
  256. MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
  257. MODULE_VERSION(DRV_VERSION);