mtk_wdt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Mediatek Watchdog Driver
  3. *
  4. * Copyright (C) 2014 Matthias Brugger
  5. *
  6. * Matthias Brugger <matthias.bgg@gmail.com>
  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/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/delay.h>
  33. #define WDT_MAX_TIMEOUT 31
  34. #define WDT_MIN_TIMEOUT 1
  35. #define WDT_LENGTH_TIMEOUT(n) ((n) << 5)
  36. #define WDT_LENGTH 0x04
  37. #define WDT_LENGTH_KEY 0x8
  38. #define WDT_RST 0x08
  39. #define WDT_RST_RELOAD 0x1971
  40. #define WDT_MODE 0x00
  41. #define WDT_MODE_EN (1 << 0)
  42. #define WDT_MODE_EXT_POL_LOW (0 << 1)
  43. #define WDT_MODE_EXT_POL_HIGH (1 << 1)
  44. #define WDT_MODE_EXRST_EN (1 << 2)
  45. #define WDT_MODE_IRQ_EN (1 << 3)
  46. #define WDT_MODE_AUTO_START (1 << 4)
  47. #define WDT_MODE_DUAL_EN (1 << 6)
  48. #define WDT_MODE_KEY 0x22000000
  49. #define WDT_SWRST 0x14
  50. #define WDT_SWRST_KEY 0x1209
  51. #define DRV_NAME "mtk-wdt"
  52. #define DRV_VERSION "1.0"
  53. static bool nowayout = WATCHDOG_NOWAYOUT;
  54. static unsigned int timeout = WDT_MAX_TIMEOUT;
  55. struct mtk_wdt_dev {
  56. struct watchdog_device wdt_dev;
  57. void __iomem *wdt_base;
  58. struct notifier_block restart_handler;
  59. };
  60. static int mtk_reset_handler(struct notifier_block *this, unsigned long mode,
  61. void *cmd)
  62. {
  63. struct mtk_wdt_dev *mtk_wdt;
  64. void __iomem *wdt_base;
  65. mtk_wdt = container_of(this, struct mtk_wdt_dev, restart_handler);
  66. wdt_base = mtk_wdt->wdt_base;
  67. while (1) {
  68. writel(WDT_SWRST_KEY, wdt_base + WDT_SWRST);
  69. mdelay(5);
  70. }
  71. return NOTIFY_DONE;
  72. }
  73. static int mtk_wdt_ping(struct watchdog_device *wdt_dev)
  74. {
  75. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  76. void __iomem *wdt_base = mtk_wdt->wdt_base;
  77. iowrite32(WDT_RST_RELOAD, wdt_base + WDT_RST);
  78. return 0;
  79. }
  80. static int mtk_wdt_set_timeout(struct watchdog_device *wdt_dev,
  81. unsigned int timeout)
  82. {
  83. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  84. void __iomem *wdt_base = mtk_wdt->wdt_base;
  85. u32 reg;
  86. wdt_dev->timeout = timeout;
  87. /*
  88. * One bit is the value of 512 ticks
  89. * The clock has 32 KHz
  90. */
  91. reg = WDT_LENGTH_TIMEOUT(timeout << 6) | WDT_LENGTH_KEY;
  92. iowrite32(reg, wdt_base + WDT_LENGTH);
  93. mtk_wdt_ping(wdt_dev);
  94. return 0;
  95. }
  96. static int mtk_wdt_stop(struct watchdog_device *wdt_dev)
  97. {
  98. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  99. void __iomem *wdt_base = mtk_wdt->wdt_base;
  100. u32 reg;
  101. reg = readl(wdt_base + WDT_MODE);
  102. reg &= ~WDT_MODE_EN;
  103. reg |= WDT_MODE_KEY;
  104. iowrite32(reg, wdt_base + WDT_MODE);
  105. return 0;
  106. }
  107. static int mtk_wdt_start(struct watchdog_device *wdt_dev)
  108. {
  109. u32 reg;
  110. struct mtk_wdt_dev *mtk_wdt = watchdog_get_drvdata(wdt_dev);
  111. void __iomem *wdt_base = mtk_wdt->wdt_base;
  112. int ret;
  113. ret = mtk_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  114. if (ret < 0)
  115. return ret;
  116. reg = ioread32(wdt_base + WDT_MODE);
  117. reg &= ~(WDT_MODE_IRQ_EN | WDT_MODE_DUAL_EN);
  118. reg |= (WDT_MODE_EN | WDT_MODE_KEY);
  119. iowrite32(reg, wdt_base + WDT_MODE);
  120. return 0;
  121. }
  122. static const struct watchdog_info mtk_wdt_info = {
  123. .identity = DRV_NAME,
  124. .options = WDIOF_SETTIMEOUT |
  125. WDIOF_KEEPALIVEPING |
  126. WDIOF_MAGICCLOSE,
  127. };
  128. static const struct watchdog_ops mtk_wdt_ops = {
  129. .owner = THIS_MODULE,
  130. .start = mtk_wdt_start,
  131. .stop = mtk_wdt_stop,
  132. .ping = mtk_wdt_ping,
  133. .set_timeout = mtk_wdt_set_timeout,
  134. };
  135. static int mtk_wdt_probe(struct platform_device *pdev)
  136. {
  137. struct mtk_wdt_dev *mtk_wdt;
  138. struct resource *res;
  139. int err;
  140. mtk_wdt = devm_kzalloc(&pdev->dev, sizeof(*mtk_wdt), GFP_KERNEL);
  141. if (!mtk_wdt)
  142. return -ENOMEM;
  143. platform_set_drvdata(pdev, mtk_wdt);
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. mtk_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  146. if (IS_ERR(mtk_wdt->wdt_base))
  147. return PTR_ERR(mtk_wdt->wdt_base);
  148. mtk_wdt->wdt_dev.info = &mtk_wdt_info;
  149. mtk_wdt->wdt_dev.ops = &mtk_wdt_ops;
  150. mtk_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
  151. mtk_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
  152. mtk_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
  153. mtk_wdt->wdt_dev.parent = &pdev->dev;
  154. watchdog_init_timeout(&mtk_wdt->wdt_dev, timeout, &pdev->dev);
  155. watchdog_set_nowayout(&mtk_wdt->wdt_dev, nowayout);
  156. watchdog_set_drvdata(&mtk_wdt->wdt_dev, mtk_wdt);
  157. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  158. err = watchdog_register_device(&mtk_wdt->wdt_dev);
  159. if (unlikely(err))
  160. return err;
  161. mtk_wdt->restart_handler.notifier_call = mtk_reset_handler;
  162. mtk_wdt->restart_handler.priority = 128;
  163. err = register_restart_handler(&mtk_wdt->restart_handler);
  164. if (err)
  165. dev_warn(&pdev->dev,
  166. "cannot register restart handler (err=%d)\n", err);
  167. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)\n",
  168. mtk_wdt->wdt_dev.timeout, nowayout);
  169. return 0;
  170. }
  171. static void mtk_wdt_shutdown(struct platform_device *pdev)
  172. {
  173. struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
  174. if (watchdog_active(&mtk_wdt->wdt_dev))
  175. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  176. }
  177. static int mtk_wdt_remove(struct platform_device *pdev)
  178. {
  179. struct mtk_wdt_dev *mtk_wdt = platform_get_drvdata(pdev);
  180. unregister_restart_handler(&mtk_wdt->restart_handler);
  181. watchdog_unregister_device(&mtk_wdt->wdt_dev);
  182. return 0;
  183. }
  184. #ifdef CONFIG_PM_SLEEP
  185. static int mtk_wdt_suspend(struct device *dev)
  186. {
  187. struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
  188. if (watchdog_active(&mtk_wdt->wdt_dev))
  189. mtk_wdt_stop(&mtk_wdt->wdt_dev);
  190. return 0;
  191. }
  192. static int mtk_wdt_resume(struct device *dev)
  193. {
  194. struct mtk_wdt_dev *mtk_wdt = dev_get_drvdata(dev);
  195. if (watchdog_active(&mtk_wdt->wdt_dev)) {
  196. mtk_wdt_start(&mtk_wdt->wdt_dev);
  197. mtk_wdt_ping(&mtk_wdt->wdt_dev);
  198. }
  199. return 0;
  200. }
  201. #endif
  202. static const struct of_device_id mtk_wdt_dt_ids[] = {
  203. { .compatible = "mediatek,mt6589-wdt" },
  204. { /* sentinel */ }
  205. };
  206. MODULE_DEVICE_TABLE(of, mtk_wdt_dt_ids);
  207. static const struct dev_pm_ops mtk_wdt_pm_ops = {
  208. SET_SYSTEM_SLEEP_PM_OPS(mtk_wdt_suspend,
  209. mtk_wdt_resume)
  210. };
  211. static struct platform_driver mtk_wdt_driver = {
  212. .probe = mtk_wdt_probe,
  213. .remove = mtk_wdt_remove,
  214. .shutdown = mtk_wdt_shutdown,
  215. .driver = {
  216. .name = DRV_NAME,
  217. .pm = &mtk_wdt_pm_ops,
  218. .of_match_table = mtk_wdt_dt_ids,
  219. },
  220. };
  221. module_platform_driver(mtk_wdt_driver);
  222. module_param(timeout, uint, 0);
  223. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  224. module_param(nowayout, bool, 0);
  225. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  226. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  227. MODULE_LICENSE("GPL");
  228. MODULE_AUTHOR("Matthias Brugger <matthias.bgg@gmail.com>");
  229. MODULE_DESCRIPTION("Mediatek WatchDog Timer Driver");
  230. MODULE_VERSION(DRV_VERSION);