sunxi_wdt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/platform_device.h>
  26. #include <linux/reboot.h>
  27. #include <linux/types.h>
  28. #include <linux/watchdog.h>
  29. #define WDT_MAX_TIMEOUT 16
  30. #define WDT_MIN_TIMEOUT 1
  31. #define WDT_MODE_TIMEOUT(n) ((n) << 3)
  32. #define WDT_TIMEOUT_MASK WDT_MODE_TIMEOUT(0x0F)
  33. #define WDT_CTRL 0x00
  34. #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
  35. #define WDT_MODE 0x04
  36. #define WDT_MODE_EN (1 << 0)
  37. #define WDT_MODE_RST_EN (1 << 1)
  38. #define DRV_NAME "sunxi-wdt"
  39. #define DRV_VERSION "1.0"
  40. static bool nowayout = WATCHDOG_NOWAYOUT;
  41. static unsigned int timeout = WDT_MAX_TIMEOUT;
  42. struct sunxi_wdt_dev {
  43. struct watchdog_device wdt_dev;
  44. void __iomem *wdt_base;
  45. struct notifier_block restart_handler;
  46. };
  47. /*
  48. * wdt_timeout_map maps the watchdog timer interval value in seconds to
  49. * the value of the register WDT_MODE bit 3:6
  50. *
  51. * [timeout seconds] = register value
  52. *
  53. */
  54. static const int wdt_timeout_map[] = {
  55. [1] = 0x1, /* 1s */
  56. [2] = 0x2, /* 2s */
  57. [3] = 0x3, /* 3s */
  58. [4] = 0x4, /* 4s */
  59. [5] = 0x5, /* 5s */
  60. [6] = 0x6, /* 6s */
  61. [8] = 0x7, /* 8s */
  62. [10] = 0x8, /* 10s */
  63. [12] = 0x9, /* 12s */
  64. [14] = 0xA, /* 14s */
  65. [16] = 0xB, /* 16s */
  66. };
  67. static int sunxi_restart_handle(struct notifier_block *this, unsigned long mode,
  68. void *cmd)
  69. {
  70. struct sunxi_wdt_dev *sunxi_wdt = container_of(this,
  71. struct sunxi_wdt_dev,
  72. restart_handler);
  73. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  74. /* Enable timer and set reset bit in the watchdog */
  75. writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
  76. /*
  77. * Restart the watchdog. The default (and lowest) interval
  78. * value for the watchdog is 0.5s.
  79. */
  80. writel(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
  81. while (1) {
  82. mdelay(5);
  83. writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
  84. }
  85. return NOTIFY_DONE;
  86. }
  87. static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
  88. {
  89. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  90. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  91. iowrite32(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
  92. return 0;
  93. }
  94. static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
  95. unsigned int timeout)
  96. {
  97. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  98. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  99. u32 reg;
  100. if (wdt_timeout_map[timeout] == 0)
  101. timeout++;
  102. sunxi_wdt->wdt_dev.timeout = timeout;
  103. reg = ioread32(wdt_base + WDT_MODE);
  104. reg &= ~WDT_TIMEOUT_MASK;
  105. reg |= WDT_MODE_TIMEOUT(wdt_timeout_map[timeout]);
  106. iowrite32(reg, wdt_base + WDT_MODE);
  107. sunxi_wdt_ping(wdt_dev);
  108. return 0;
  109. }
  110. static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
  111. {
  112. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  113. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  114. iowrite32(0, wdt_base + WDT_MODE);
  115. return 0;
  116. }
  117. static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
  118. {
  119. u32 reg;
  120. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  121. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  122. int ret;
  123. ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
  124. sunxi_wdt->wdt_dev.timeout);
  125. if (ret < 0)
  126. return ret;
  127. reg = ioread32(wdt_base + WDT_MODE);
  128. reg |= (WDT_MODE_RST_EN | WDT_MODE_EN);
  129. iowrite32(reg, wdt_base + WDT_MODE);
  130. return 0;
  131. }
  132. static const struct watchdog_info sunxi_wdt_info = {
  133. .identity = DRV_NAME,
  134. .options = WDIOF_SETTIMEOUT |
  135. WDIOF_KEEPALIVEPING |
  136. WDIOF_MAGICCLOSE,
  137. };
  138. static const struct watchdog_ops sunxi_wdt_ops = {
  139. .owner = THIS_MODULE,
  140. .start = sunxi_wdt_start,
  141. .stop = sunxi_wdt_stop,
  142. .ping = sunxi_wdt_ping,
  143. .set_timeout = sunxi_wdt_set_timeout,
  144. };
  145. static int sunxi_wdt_probe(struct platform_device *pdev)
  146. {
  147. struct sunxi_wdt_dev *sunxi_wdt;
  148. struct resource *res;
  149. int err;
  150. sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL);
  151. if (!sunxi_wdt)
  152. return -EINVAL;
  153. platform_set_drvdata(pdev, sunxi_wdt);
  154. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  155. sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  156. if (IS_ERR(sunxi_wdt->wdt_base))
  157. return PTR_ERR(sunxi_wdt->wdt_base);
  158. sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
  159. sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
  160. sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
  161. sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
  162. sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
  163. sunxi_wdt->wdt_dev.parent = &pdev->dev;
  164. watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev);
  165. watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
  166. watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
  167. sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
  168. err = watchdog_register_device(&sunxi_wdt->wdt_dev);
  169. if (unlikely(err))
  170. return err;
  171. sunxi_wdt->restart_handler.notifier_call = sunxi_restart_handle;
  172. sunxi_wdt->restart_handler.priority = 128;
  173. err = register_restart_handler(&sunxi_wdt->restart_handler);
  174. if (err)
  175. dev_err(&pdev->dev,
  176. "cannot register restart handler (err=%d)\n", err);
  177. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  178. sunxi_wdt->wdt_dev.timeout, nowayout);
  179. return 0;
  180. }
  181. static int sunxi_wdt_remove(struct platform_device *pdev)
  182. {
  183. struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
  184. unregister_restart_handler(&sunxi_wdt->restart_handler);
  185. watchdog_unregister_device(&sunxi_wdt->wdt_dev);
  186. watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
  187. return 0;
  188. }
  189. static void sunxi_wdt_shutdown(struct platform_device *pdev)
  190. {
  191. struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
  192. sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
  193. }
  194. static const struct of_device_id sunxi_wdt_dt_ids[] = {
  195. { .compatible = "allwinner,sun4i-a10-wdt" },
  196. { /* sentinel */ }
  197. };
  198. MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
  199. static struct platform_driver sunxi_wdt_driver = {
  200. .probe = sunxi_wdt_probe,
  201. .remove = sunxi_wdt_remove,
  202. .shutdown = sunxi_wdt_shutdown,
  203. .driver = {
  204. .owner = THIS_MODULE,
  205. .name = DRV_NAME,
  206. .of_match_table = sunxi_wdt_dt_ids,
  207. },
  208. };
  209. module_platform_driver(sunxi_wdt_driver);
  210. module_param(timeout, uint, 0);
  211. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  212. module_param(nowayout, bool, 0);
  213. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  214. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
  217. MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
  218. MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
  219. MODULE_VERSION(DRV_VERSION);