sunxi_wdt.c 8.6 KB

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