meson_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Meson Watchdog Driver
  3. *
  4. * Copyright (c) 2014 Carlo Caione
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/notifier.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/reboot.h>
  23. #include <linux/types.h>
  24. #include <linux/watchdog.h>
  25. #define DRV_NAME "meson_wdt"
  26. #define MESON_WDT_TC 0x00
  27. #define MESON_WDT_TC_EN BIT(22)
  28. #define MESON_WDT_TC_TM_MASK 0x3fffff
  29. #define MESON_WDT_DC_RESET (3 << 24)
  30. #define MESON_WDT_RESET 0x04
  31. #define MESON_WDT_TIMEOUT 30
  32. #define MESON_WDT_MIN_TIMEOUT 1
  33. #define MESON_WDT_MAX_TIMEOUT (MESON_WDT_TC_TM_MASK / 100000)
  34. #define MESON_SEC_TO_TC(s) ((s) * 100000)
  35. static bool nowayout = WATCHDOG_NOWAYOUT;
  36. static unsigned int timeout = MESON_WDT_TIMEOUT;
  37. struct meson_wdt_dev {
  38. struct watchdog_device wdt_dev;
  39. void __iomem *wdt_base;
  40. struct notifier_block restart_handler;
  41. };
  42. static int meson_restart_handle(struct notifier_block *this, unsigned long mode,
  43. void *cmd)
  44. {
  45. u32 tc_reboot = MESON_WDT_DC_RESET | MESON_WDT_TC_EN;
  46. struct meson_wdt_dev *meson_wdt = container_of(this,
  47. struct meson_wdt_dev,
  48. restart_handler);
  49. while (1) {
  50. writel(tc_reboot, meson_wdt->wdt_base + MESON_WDT_TC);
  51. mdelay(5);
  52. }
  53. return NOTIFY_DONE;
  54. }
  55. static int meson_wdt_ping(struct watchdog_device *wdt_dev)
  56. {
  57. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  58. writel(0, meson_wdt->wdt_base + MESON_WDT_RESET);
  59. return 0;
  60. }
  61. static void meson_wdt_change_timeout(struct watchdog_device *wdt_dev,
  62. unsigned int timeout)
  63. {
  64. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  65. u32 reg;
  66. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  67. reg &= ~MESON_WDT_TC_TM_MASK;
  68. reg |= MESON_SEC_TO_TC(timeout);
  69. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  70. }
  71. static int meson_wdt_set_timeout(struct watchdog_device *wdt_dev,
  72. unsigned int timeout)
  73. {
  74. wdt_dev->timeout = timeout;
  75. meson_wdt_change_timeout(wdt_dev, timeout);
  76. meson_wdt_ping(wdt_dev);
  77. return 0;
  78. }
  79. static int meson_wdt_stop(struct watchdog_device *wdt_dev)
  80. {
  81. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  82. u32 reg;
  83. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  84. reg &= ~MESON_WDT_TC_EN;
  85. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  86. return 0;
  87. }
  88. static int meson_wdt_start(struct watchdog_device *wdt_dev)
  89. {
  90. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  91. u32 reg;
  92. meson_wdt_change_timeout(wdt_dev, meson_wdt->wdt_dev.timeout);
  93. meson_wdt_ping(wdt_dev);
  94. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  95. reg |= MESON_WDT_TC_EN;
  96. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  97. return 0;
  98. }
  99. static const struct watchdog_info meson_wdt_info = {
  100. .identity = DRV_NAME,
  101. .options = WDIOF_SETTIMEOUT |
  102. WDIOF_KEEPALIVEPING |
  103. WDIOF_MAGICCLOSE,
  104. };
  105. static const struct watchdog_ops meson_wdt_ops = {
  106. .owner = THIS_MODULE,
  107. .start = meson_wdt_start,
  108. .stop = meson_wdt_stop,
  109. .ping = meson_wdt_ping,
  110. .set_timeout = meson_wdt_set_timeout,
  111. };
  112. static int meson_wdt_probe(struct platform_device *pdev)
  113. {
  114. struct resource *res;
  115. struct meson_wdt_dev *meson_wdt;
  116. int err;
  117. meson_wdt = devm_kzalloc(&pdev->dev, sizeof(*meson_wdt), GFP_KERNEL);
  118. if (!meson_wdt)
  119. return -ENOMEM;
  120. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  121. meson_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  122. if (IS_ERR(meson_wdt->wdt_base))
  123. return PTR_ERR(meson_wdt->wdt_base);
  124. meson_wdt->wdt_dev.parent = &pdev->dev;
  125. meson_wdt->wdt_dev.info = &meson_wdt_info;
  126. meson_wdt->wdt_dev.ops = &meson_wdt_ops;
  127. meson_wdt->wdt_dev.timeout = MESON_WDT_TIMEOUT;
  128. meson_wdt->wdt_dev.max_timeout = MESON_WDT_MAX_TIMEOUT;
  129. meson_wdt->wdt_dev.min_timeout = MESON_WDT_MIN_TIMEOUT;
  130. watchdog_set_drvdata(&meson_wdt->wdt_dev, meson_wdt);
  131. watchdog_init_timeout(&meson_wdt->wdt_dev, timeout, &pdev->dev);
  132. watchdog_set_nowayout(&meson_wdt->wdt_dev, nowayout);
  133. meson_wdt_stop(&meson_wdt->wdt_dev);
  134. err = watchdog_register_device(&meson_wdt->wdt_dev);
  135. if (err)
  136. return err;
  137. platform_set_drvdata(pdev, meson_wdt);
  138. meson_wdt->restart_handler.notifier_call = meson_restart_handle;
  139. meson_wdt->restart_handler.priority = 128;
  140. err = register_restart_handler(&meson_wdt->restart_handler);
  141. if (err)
  142. dev_err(&pdev->dev,
  143. "cannot register restart handler (err=%d)\n", err);
  144. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  145. meson_wdt->wdt_dev.timeout, nowayout);
  146. return 0;
  147. }
  148. static int meson_wdt_remove(struct platform_device *pdev)
  149. {
  150. struct meson_wdt_dev *meson_wdt = platform_get_drvdata(pdev);
  151. unregister_restart_handler(&meson_wdt->restart_handler);
  152. watchdog_unregister_device(&meson_wdt->wdt_dev);
  153. return 0;
  154. }
  155. static void meson_wdt_shutdown(struct platform_device *pdev)
  156. {
  157. struct meson_wdt_dev *meson_wdt = platform_get_drvdata(pdev);
  158. meson_wdt_stop(&meson_wdt->wdt_dev);
  159. }
  160. static const struct of_device_id meson_wdt_dt_ids[] = {
  161. { .compatible = "amlogic,meson6-wdt" },
  162. { /* sentinel */ }
  163. };
  164. MODULE_DEVICE_TABLE(of, meson_wdt_dt_ids);
  165. static struct platform_driver meson_wdt_driver = {
  166. .probe = meson_wdt_probe,
  167. .remove = meson_wdt_remove,
  168. .shutdown = meson_wdt_shutdown,
  169. .driver = {
  170. .name = DRV_NAME,
  171. .of_match_table = meson_wdt_dt_ids,
  172. },
  173. };
  174. module_platform_driver(meson_wdt_driver);
  175. module_param(timeout, uint, 0);
  176. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  177. module_param(nowayout, bool, 0);
  178. MODULE_PARM_DESC(nowayout,
  179. "Watchdog cannot be stopped once started (default="
  180. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  181. MODULE_LICENSE("GPL");
  182. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  183. MODULE_DESCRIPTION("Meson Watchdog Timer Driver");