mtk_wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Mediatek Watchdog Driver
  3. *
  4. * Copyright (C) 2014 Matthias Brugger
  5. *
  6. * Matthias Brugger <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * Based on sunxi_wdt.c
  19. */
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/types.h>
  29. #include <linux/watchdog.h>
  30. #include <linux/delay.h>
  31. #define WDT_MAX_TIMEOUT 31
  32. #define WDT_MIN_TIMEOUT 1
  33. #define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
  34. #define WDT_LENGTH 0x04
  35. #define WDT_LENGTH_KEY 0x8
  36. #define WDT_RST 0x08
  37. #define WDT_RST_RELOAD 0x1971
  38. #define WDT_MODE 0x00
  39. #define WDT_MODE_EN (1 << 0)
  40. #define WDT_MODE_EXT_POL_LOW (0 << 1)
  41. #define WDT_MODE_EXT_POL_HIGH (1 << 1)
  42. #define WDT_MODE_EXRST_EN (1 << 2)
  43. #define WDT_MODE_IRQ_EN (1 << 3)
  44. #define WDT_MODE_AUTO_START (1 << 4)
  45. #define WDT_MODE_DUAL_EN (1 << 6)
  46. #define WDT_MODE_KEY 0x22000000
  47. #define WDT_SWRST 0x14
  48. #define WDT_SWRST_KEY 0x1209
  49. #define DRV_NAME "mtk-wdt"
  50. #define DRV_VERSION "1.0"
  51. static bool nowayout = WATCHDOG_NOWAYOUT;
  52. static unsigned int timeout = WDT_MAX_TIMEOUT;
  53. struct mtk_wdt_dev {
  54. struct watchdog_device wdt_dev;
  55. void __iomem *wdt_base;
  56. };
  57. static int mtk_wdt_restart(struct watchdog_device *wdt_dev,
  58. unsigned long action, void *data)
  59. {
  60. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  61. void __iomem *wdt_base;
  62. wdt_base = mtk_wdt->wdt_base;
  63. while (1) {
  64. writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
  65. mdelay(5);
  66. }
  67. return 0;
  68. }
  69. static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
  70. {
  71. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  72. void __iomem *wdt_base = mtk_wdt->wdt_base;
  73. iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
  74. return 0;
  75. }
  76. static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
  77. unsigned int timeout)
  78. {
  79. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  80. void __iomem *wdt_base = mtk_wdt->wdt_base;
  81. u32 reg;
  82. wdt_dev->timeout = timeout;
  83. /*
  84. * One bit is the value of 512 ticks
  85. * The clock has 32 KHz
  86. */
  87. reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY;
  88. iowrite32(reg, wdt_base + WDT_LENGTH);
  89. mtk_wdt_ping(wdt_dev);
  90. return 0;
  91. }
  92. static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
  93. {
  94. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  95. void __iomem *wdt_base = mtk_wdt->wdt_base;
  96. u32 reg;
  97. reg = readl(wdt_base + WDT_MODE);
  98. reg &= ~WDT_MODE_EN;
  99. reg |= WDT_MODE_KEY;
  100. iowrite32(reg, wdt_base + WDT_MODE);
  101. return 0;
  102. }
  103. static int mtk_wdt_start(struct watchdog_device *wdt_dev)
  104. {
  105. u32 reg;
  106. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  107. void __iomem *wdt_base = mtk_wdt->wdt_base;
  108. int ret;
  109. ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  110. if (ret < 0)
  111. return ret;
  112. reg = ioread32(wdt_base + WDT_MODE);
  113. reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
  114. reg |= (WDT_MODE_EN | WDT_MODE_KEY);
  115. iowrite32(reg, wdt_base + WDT_MODE);
  116. return 0;
  117. }
  118. static const struct watchdog_info mtk_wdt_info = {
  119. .identity = DRV_NAME,
  120. .options = WDIOF_SETTIMEOUT |
  121. WDIOF_KEEPALIVEPING |
  122. WDIOF_MAGICCLOSE,
  123. };
  124. static const struct watchdog_ops mtk_wdt_ops = {
  125. .owner = THIS_MODULE,
  126. .start = mtk_wdt_start,
  127. .stop = mtk_wdt_stop,
  128. .ping = mtk_wdt_ping,
  129. .set_timeout = mtk_wdt_set_timeout,
  130. .restart = mtk_wdt_restart,
  131. };
  132. static int mtk_wdt_probe(struct platform_device *pdev)
  133. {
  134. struct mtk_wdt_dev *mtk_wdt;
  135. struct resource *res;
  136. int err;
  137. mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL);
  138. if (!mtk_wdt)
  139. return -ENOMEM;
  140. platform_set_drvdata(pdev, mtk_wdt);
  141. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  142. mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  143. if (IS_ERR(mtk_wdt->wdt_base))
  144. return PTR_ERR(mtk_wdt->wdt_base);
  145. mtk_wdt->wdt_dev.info = &mtk_wdt_info;
  146. mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
  147. mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
  148. mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
  149. mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
  150. mtk_wdt->wdt_dev.parent = &pdev->dev;
  151. watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev);
  152. watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
  153. watchdog_set_restart_priority(&mtk_wdt->wdt_dev, 128);
  154. watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
  155. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  156. err = watchdog_register_device(&mtk_wdt->wdt_dev);
  157. if (unlikely(err))
  158. return err;
  159. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
  160. mtk_wdt->wdt_dev.timeout, nowayout);
  161. return 0;
  162. }
  163. static void mtk_wdt_shutdown(struct platform_device *pdev)
  164. {
  165. struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
  166. if (watchdog_active(&mtk_wdt->wdt_dev))
  167. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  168. }
  169. static int mtk_wdt_remove(struct platform_device *pdev)
  170. {
  171. struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
  172. watchdog_unregister_device(&mtk_wdt->wdt_dev);
  173. return 0;
  174. }
  175. #ifdef CONFIG_PM_SLEEP
  176. static int mtk_wdt_suspend(struct device *dev)
  177. {
  178. struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
  179. if (watchdog_active(&mtk_wdt->wdt_dev))
  180. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  181. return 0;
  182. }
  183. static int mtk_wdt_resume(struct device *dev)
  184. {
  185. struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
  186. if (watchdog_active(&mtk_wdt->wdt_dev)) {
  187. mtk_wdt_start(&mtk_wdt->wdt_dev);
  188. mtk_wdt_ping(&mtk_wdt->wdt_dev);
  189. }
  190. return 0;
  191. }
  192. #endif
  193. static const struct of_device_id mtk_wdt_dt_ids[] = {
  194. { .compatible = "mediatek,mt6589-wdt" },
  195. { /* sentinel */ }
  196. };
  197. MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
  198. static const struct dev_pm_ops mtk_wdt_pm_ops = {
  199. SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend,
  200. mtk_wdt_resume)
  201. };
  202. static struct platform_driver mtk_wdt_driver = {
  203. .probe = mtk_wdt_probe,
  204. .remove = mtk_wdt_remove,
  205. .shutdown = mtk_wdt_shutdown,
  206. .driver = {
  207. .name = DRV_NAME,
  208. .pm = &mtk_wdt_pm_ops,
  209. .of_match_table = mtk_wdt_dt_ids,
  210. },
  211. };
  212. module_platform_driver(mtk_wdt_driver);
  213. module_param(timeout, uint, 0);
  214. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  215. module_param(nowayout, bool, 0);
  216. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  217. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  218. MODULE_LICENSE("GPL");
  219. MODULE_AUTHOR("Matthias Brugger <[email protected]>");
  220. MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
  221. MODULE_VERSION(DRV_VERSION);