meson_wdt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Meson Watchdog Driver
  4. *
  5. * Copyright (c) 2014 Carlo Caione
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #include <linux/watchdog.h>
  20. #define DRV_NAME "meson_wdt"
  21. #define MESON_WDT_TC 0x00
  22. #define MESON_WDT_DC_RESET (3 << 24)
  23. #define MESON_WDT_RESET 0x04
  24. #define MESON_WDT_TIMEOUT 30
  25. #define MESON_WDT_MIN_TIMEOUT 1
  26. #define MESON_SEC_TO_TC(s, c) ((s) * (c))
  27. static bool nowayout = WATCHDOG_NOWAYOUT;
  28. static unsigned int timeout;
  29. struct meson_wdt_data {
  30. unsigned int enable;
  31. unsigned int terminal_count_mask;
  32. unsigned int count_unit;
  33. };
  34. static struct meson_wdt_data meson6_wdt_data = {
  35. .enable = BIT(22),
  36. .terminal_count_mask = 0x3fffff,
  37. .count_unit = 100000, /* 10 us */
  38. };
  39. static struct meson_wdt_data meson8b_wdt_data = {
  40. .enable = BIT(19),
  41. .terminal_count_mask = 0xffff,
  42. .count_unit = 7812, /* 128 us */
  43. };
  44. struct meson_wdt_dev {
  45. struct watchdog_device wdt_dev;
  46. void __iomem *wdt_base;
  47. const struct meson_wdt_data *data;
  48. };
  49. static int meson_wdt_restart(struct watchdog_device *wdt_dev,
  50. unsigned long action, void *data)
  51. {
  52. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  53. u32 tc_reboot = MESON_WDT_DC_RESET;
  54. tc_reboot |= meson_wdt->data->enable;
  55. while (1) {
  56. writel(tc_reboot, meson_wdt->wdt_base + MESON_WDT_TC);
  57. mdelay(5);
  58. }
  59. return 0;
  60. }
  61. static int meson_wdt_ping(struct watchdog_device *wdt_dev)
  62. {
  63. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  64. writel(0, meson_wdt->wdt_base + MESON_WDT_RESET);
  65. return 0;
  66. }
  67. static void meson_wdt_change_timeout(struct watchdog_device *wdt_dev,
  68. unsigned int timeout)
  69. {
  70. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  71. u32 reg;
  72. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  73. reg &= ~meson_wdt->data->terminal_count_mask;
  74. reg |= MESON_SEC_TO_TC(timeout, meson_wdt->data->count_unit);
  75. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  76. }
  77. static int meson_wdt_set_timeout(struct watchdog_device *wdt_dev,
  78. unsigned int timeout)
  79. {
  80. wdt_dev->timeout = timeout;
  81. meson_wdt_change_timeout(wdt_dev, timeout);
  82. meson_wdt_ping(wdt_dev);
  83. return 0;
  84. }
  85. static int meson_wdt_stop(struct watchdog_device *wdt_dev)
  86. {
  87. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  88. u32 reg;
  89. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  90. reg &= ~meson_wdt->data->enable;
  91. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  92. return 0;
  93. }
  94. static int meson_wdt_start(struct watchdog_device *wdt_dev)
  95. {
  96. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  97. u32 reg;
  98. meson_wdt_change_timeout(wdt_dev, meson_wdt->wdt_dev.timeout);
  99. meson_wdt_ping(wdt_dev);
  100. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  101. reg |= meson_wdt->data->enable;
  102. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  103. return 0;
  104. }
  105. static const struct watchdog_info meson_wdt_info = {
  106. .identity = DRV_NAME,
  107. .options = WDIOF_SETTIMEOUT |
  108. WDIOF_KEEPALIVEPING |
  109. WDIOF_MAGICCLOSE,
  110. };
  111. static const struct watchdog_ops meson_wdt_ops = {
  112. .owner = THIS_MODULE,
  113. .start = meson_wdt_start,
  114. .stop = meson_wdt_stop,
  115. .ping = meson_wdt_ping,
  116. .set_timeout = meson_wdt_set_timeout,
  117. .restart = meson_wdt_restart,
  118. };
  119. static const struct of_device_id meson_wdt_dt_ids[] = {
  120. { .compatible = "amlogic,meson6-wdt", .data = &meson6_wdt_data },
  121. { .compatible = "amlogic,meson8-wdt", .data = &meson6_wdt_data },
  122. { .compatible = "amlogic,meson8b-wdt", .data = &meson8b_wdt_data },
  123. { .compatible = "amlogic,meson8m2-wdt", .data = &meson8b_wdt_data },
  124. { /* sentinel */ }
  125. };
  126. MODULE_DEVICE_TABLE(of, meson_wdt_dt_ids);
  127. static int meson_wdt_probe(struct platform_device *pdev)
  128. {
  129. struct device *dev = &pdev->dev;
  130. struct meson_wdt_dev *meson_wdt;
  131. const struct of_device_id *of_id;
  132. int err;
  133. meson_wdt = devm_kzalloc(dev, sizeof(*meson_wdt), GFP_KERNEL);
  134. if (!meson_wdt)
  135. return -ENOMEM;
  136. meson_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
  137. if (IS_ERR(meson_wdt->wdt_base))
  138. return PTR_ERR(meson_wdt->wdt_base);
  139. of_id = of_match_device(meson_wdt_dt_ids, dev);
  140. if (!of_id) {
  141. dev_err(dev, "Unable to initialize WDT data\n");
  142. return -ENODEV;
  143. }
  144. meson_wdt->data = of_id->data;
  145. meson_wdt->wdt_dev.parent = dev;
  146. meson_wdt->wdt_dev.info = &meson_wdt_info;
  147. meson_wdt->wdt_dev.ops = &meson_wdt_ops;
  148. meson_wdt->wdt_dev.max_timeout =
  149. meson_wdt->data->terminal_count_mask / meson_wdt->data->count_unit;
  150. meson_wdt->wdt_dev.min_timeout = MESON_WDT_MIN_TIMEOUT;
  151. meson_wdt->wdt_dev.timeout = min_t(unsigned int,
  152. MESON_WDT_TIMEOUT,
  153. meson_wdt->wdt_dev.max_timeout);
  154. watchdog_set_drvdata(&meson_wdt->wdt_dev, meson_wdt);
  155. watchdog_init_timeout(&meson_wdt->wdt_dev, timeout, dev);
  156. watchdog_set_nowayout(&meson_wdt->wdt_dev, nowayout);
  157. watchdog_set_restart_priority(&meson_wdt->wdt_dev, 128);
  158. meson_wdt_stop(&meson_wdt->wdt_dev);
  159. watchdog_stop_on_reboot(&meson_wdt->wdt_dev);
  160. err = devm_watchdog_register_device(dev, &meson_wdt->wdt_dev);
  161. if (err)
  162. return err;
  163. dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  164. meson_wdt->wdt_dev.timeout, nowayout);
  165. return 0;
  166. }
  167. static struct platform_driver meson_wdt_driver = {
  168. .probe = meson_wdt_probe,
  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");