sunxi_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/err.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/types.h>
  25. #include <linux/watchdog.h>
  26. #define WDT_MAX_TIMEOUT 16
  27. #define WDT_MIN_TIMEOUT 1
  28. #define WDT_MODE_TIMEOUT(n) ((n) << 3)
  29. #define WDT_TIMEOUT_MASK WDT_MODE_TIMEOUT(0x0F)
  30. #define WDT_CTRL 0x00
  31. #define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
  32. #define WDT_MODE 0x04
  33. #define WDT_MODE_EN (1 << 0)
  34. #define WDT_MODE_RST_EN (1 << 1)
  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. struct sunxi_wdt_dev {
  40. struct watchdog_device wdt_dev;
  41. void __iomem *wdt_base;
  42. };
  43. /*
  44. * wdt_timeout_map maps the watchdog timer interval value in seconds to
  45. * the value of the register WDT_MODE bit 3:6
  46. *
  47. * [timeout seconds] = register value
  48. *
  49. */
  50. static const int wdt_timeout_map[] = {
  51. [1] = 0b0001, /* 1s */
  52. [2] = 0b0010, /* 2s */
  53. [3] = 0b0011, /* 3s */
  54. [4] = 0b0100, /* 4s */
  55. [5] = 0b0101, /* 5s */
  56. [6] = 0b0110, /* 6s */
  57. [8] = 0b0111, /* 8s */
  58. [10] = 0b1000, /* 10s */
  59. [12] = 0b1001, /* 12s */
  60. [14] = 0b1010, /* 14s */
  61. [16] = 0b1011, /* 16s */
  62. };
  63. static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
  64. {
  65. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  66. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  67. iowrite32(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
  68. return 0;
  69. }
  70. static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
  71. unsigned int timeout)
  72. {
  73. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  74. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  75. u32 reg;
  76. if (wdt_timeout_map[timeout] == 0)
  77. timeout++;
  78. sunxi_wdt->wdt_dev.timeout = timeout;
  79. reg = ioread32(wdt_base + WDT_MODE);
  80. reg &= ~WDT_TIMEOUT_MASK;
  81. reg |= WDT_MODE_TIMEOUT(wdt_timeout_map[timeout]);
  82. iowrite32(reg, wdt_base + WDT_MODE);
  83. sunxi_wdt_ping(wdt_dev);
  84. return 0;
  85. }
  86. static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
  87. {
  88. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  89. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  90. iowrite32(0, wdt_base + WDT_MODE);
  91. return 0;
  92. }
  93. static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
  94. {
  95. u32 reg;
  96. struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
  97. void __iomem *wdt_base = sunxi_wdt->wdt_base;
  98. int ret;
  99. ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
  100. sunxi_wdt->wdt_dev.timeout);
  101. if (ret < 0)
  102. return ret;
  103. reg = ioread32(wdt_base + WDT_MODE);
  104. reg |= (WDT_MODE_RST_EN | WDT_MODE_EN);
  105. iowrite32(reg, wdt_base + WDT_MODE);
  106. return 0;
  107. }
  108. static const struct watchdog_info sunxi_wdt_info = {
  109. .identity = DRV_NAME,
  110. .options = WDIOF_SETTIMEOUT |
  111. WDIOF_KEEPALIVEPING |
  112. WDIOF_MAGICCLOSE,
  113. };
  114. static const struct watchdog_ops sunxi_wdt_ops = {
  115. .owner = THIS_MODULE,
  116. .start = sunxi_wdt_start,
  117. .stop = sunxi_wdt_stop,
  118. .ping = sunxi_wdt_ping,
  119. .set_timeout = sunxi_wdt_set_timeout,
  120. };
  121. static int __init sunxi_wdt_probe(struct platform_device *pdev)
  122. {
  123. struct sunxi_wdt_dev *sunxi_wdt;
  124. struct resource *res;
  125. int err;
  126. sunxi_wdt = devm_kzalloc(&pdev->dev, sizeof(*sunxi_wdt), GFP_KERNEL);
  127. if (!sunxi_wdt)
  128. return -EINVAL;
  129. platform_set_drvdata(pdev, sunxi_wdt);
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. sunxi_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  132. if (IS_ERR(sunxi_wdt->wdt_base))
  133. return PTR_ERR(sunxi_wdt->wdt_base);
  134. sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
  135. sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
  136. sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
  137. sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
  138. sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
  139. sunxi_wdt->wdt_dev.parent = &pdev->dev;
  140. watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, &pdev->dev);
  141. watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
  142. watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
  143. sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
  144. err = watchdog_register_device(&sunxi_wdt->wdt_dev);
  145. if (unlikely(err))
  146. return err;
  147. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  148. sunxi_wdt->wdt_dev.timeout, nowayout);
  149. return 0;
  150. }
  151. static int __exit sunxi_wdt_remove(struct platform_device *pdev)
  152. {
  153. struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
  154. watchdog_unregister_device(&sunxi_wdt->wdt_dev);
  155. watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);
  156. return 0;
  157. }
  158. static void sunxi_wdt_shutdown(struct platform_device *pdev)
  159. {
  160. struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
  161. sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
  162. }
  163. static const struct of_device_id sunxi_wdt_dt_ids[] = {
  164. { .compatible = "allwinner,sun4i-wdt" },
  165. { /* sentinel */ }
  166. };
  167. MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
  168. static struct platform_driver sunxi_wdt_driver = {
  169. .probe = sunxi_wdt_probe,
  170. .remove = sunxi_wdt_remove,
  171. .shutdown = sunxi_wdt_shutdown,
  172. .driver = {
  173. .owner = THIS_MODULE,
  174. .name = DRV_NAME,
  175. .of_match_table = of_match_ptr(sunxi_wdt_dt_ids)
  176. },
  177. };
  178. module_platform_driver(sunxi_wdt_driver);
  179. module_param(timeout, uint, 0);
  180. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  181. module_param(nowayout, bool, 0);
  182. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  183. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  184. MODULE_LICENSE("GPL");
  185. MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
  186. MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
  187. MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
  188. MODULE_VERSION(DRV_VERSION);