sunxi_wdt.c 7.9 KB

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