mt7621_wdt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Ralink MT7621/MT7628 built-in hardware watchdog timer
  4. *
  5. * Copyright (C) 2014 John Crispin <john@phrozen.org>
  6. *
  7. * This driver was based on: drivers/watchdog/rt2880_wdt.c
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/reset.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/regmap.h>
  19. #define SYSC_RSTSTAT 0x38
  20. #define WDT_RST_CAUSE BIT(1)
  21. #define RALINK_WDT_TIMEOUT 30
  22. #define TIMER_REG_TMRSTAT 0x00
  23. #define TIMER_REG_TMR1LOAD 0x24
  24. #define TIMER_REG_TMR1CTL 0x20
  25. #define TMR1CTL_ENABLE BIT(7)
  26. #define TMR1CTL_RESTART BIT(9)
  27. #define TMR1CTL_PRESCALE_SHIFT 16
  28. struct mt7621_wdt_data {
  29. void __iomem *base;
  30. struct reset_control *rst;
  31. struct regmap *sysc;
  32. struct watchdog_device wdt;
  33. };
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. module_param(nowayout, bool, 0);
  36. MODULE_PARM_DESC(nowayout,
  37. "Watchdog cannot be stopped once started (default="
  38. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  39. static inline void rt_wdt_w32(void __iomem *base, unsigned int reg, u32 val)
  40. {
  41. iowrite32(val, base + reg);
  42. }
  43. static inline u32 rt_wdt_r32(void __iomem *base, unsigned int reg)
  44. {
  45. return ioread32(base + reg);
  46. }
  47. static int mt7621_wdt_ping(struct watchdog_device *w)
  48. {
  49. struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  50. rt_wdt_w32(drvdata->base, TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
  51. return 0;
  52. }
  53. static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
  54. {
  55. struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  56. w->timeout = t;
  57. rt_wdt_w32(drvdata->base, TIMER_REG_TMR1LOAD, t * 1000);
  58. mt7621_wdt_ping(w);
  59. return 0;
  60. }
  61. static int mt7621_wdt_start(struct watchdog_device *w)
  62. {
  63. struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  64. u32 t;
  65. /* set the prescaler to 1ms == 1000us */
  66. rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
  67. mt7621_wdt_set_timeout(w, w->timeout);
  68. t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
  69. t |= TMR1CTL_ENABLE;
  70. rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
  71. return 0;
  72. }
  73. static int mt7621_wdt_stop(struct watchdog_device *w)
  74. {
  75. struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  76. u32 t;
  77. mt7621_wdt_ping(w);
  78. t = rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL);
  79. t &= ~TMR1CTL_ENABLE;
  80. rt_wdt_w32(drvdata->base, TIMER_REG_TMR1CTL, t);
  81. return 0;
  82. }
  83. static int mt7621_wdt_bootcause(struct mt7621_wdt_data *d)
  84. {
  85. u32 val;
  86. regmap_read(d->sysc, SYSC_RSTSTAT, &val);
  87. if (val & WDT_RST_CAUSE)
  88. return WDIOF_CARDRESET;
  89. return 0;
  90. }
  91. static int mt7621_wdt_is_running(struct watchdog_device *w)
  92. {
  93. struct mt7621_wdt_data *drvdata = watchdog_get_drvdata(w);
  94. return !!(rt_wdt_r32(drvdata->base, TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
  95. }
  96. static const struct watchdog_info mt7621_wdt_info = {
  97. .identity = "Mediatek Watchdog",
  98. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  99. };
  100. static const struct watchdog_ops mt7621_wdt_ops = {
  101. .owner = THIS_MODULE,
  102. .start = mt7621_wdt_start,
  103. .stop = mt7621_wdt_stop,
  104. .ping = mt7621_wdt_ping,
  105. .set_timeout = mt7621_wdt_set_timeout,
  106. };
  107. static int mt7621_wdt_probe(struct platform_device *pdev)
  108. {
  109. struct device_node *np = pdev->dev.of_node;
  110. struct device *dev = &pdev->dev;
  111. struct watchdog_device *mt7621_wdt;
  112. struct mt7621_wdt_data *drvdata;
  113. int err;
  114. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  115. if (!drvdata)
  116. return -ENOMEM;
  117. drvdata->sysc = syscon_regmap_lookup_by_phandle(np, "mediatek,sysctl");
  118. if (IS_ERR(drvdata->sysc)) {
  119. drvdata->sysc = syscon_regmap_lookup_by_compatible("mediatek,mt7621-sysc");
  120. if (IS_ERR(drvdata->sysc))
  121. return PTR_ERR(drvdata->sysc);
  122. }
  123. drvdata->base = devm_platform_ioremap_resource(pdev, 0);
  124. if (IS_ERR(drvdata->base))
  125. return PTR_ERR(drvdata->base);
  126. drvdata->rst = devm_reset_control_get_exclusive(dev, NULL);
  127. if (!IS_ERR(drvdata->rst))
  128. reset_control_deassert(drvdata->rst);
  129. mt7621_wdt = &drvdata->wdt;
  130. mt7621_wdt->info = &mt7621_wdt_info;
  131. mt7621_wdt->ops = &mt7621_wdt_ops;
  132. mt7621_wdt->min_timeout = 1;
  133. mt7621_wdt->max_timeout = 0xfffful / 1000;
  134. mt7621_wdt->parent = dev;
  135. mt7621_wdt->bootstatus = mt7621_wdt_bootcause(drvdata);
  136. watchdog_init_timeout(mt7621_wdt, mt7621_wdt->max_timeout, dev);
  137. watchdog_set_nowayout(mt7621_wdt, nowayout);
  138. watchdog_set_drvdata(mt7621_wdt, drvdata);
  139. if (mt7621_wdt_is_running(mt7621_wdt)) {
  140. /*
  141. * Make sure to apply timeout from watchdog core, taking
  142. * the prescaler of this driver here into account (the
  143. * boot loader might be using a different prescaler).
  144. *
  145. * To avoid spurious resets because of different scaling,
  146. * we first disable the watchdog, set the new prescaler
  147. * and timeout, and then re-enable the watchdog.
  148. */
  149. mt7621_wdt_stop(mt7621_wdt);
  150. mt7621_wdt_start(mt7621_wdt);
  151. set_bit(WDOG_HW_RUNNING, &mt7621_wdt->status);
  152. }
  153. err = devm_watchdog_register_device(dev, &drvdata->wdt);
  154. if (err)
  155. return err;
  156. platform_set_drvdata(pdev, drvdata);
  157. return 0;
  158. }
  159. static void mt7621_wdt_shutdown(struct platform_device *pdev)
  160. {
  161. struct mt7621_wdt_data *drvdata = platform_get_drvdata(pdev);
  162. mt7621_wdt_stop(&drvdata->wdt);
  163. }
  164. static const struct of_device_id mt7621_wdt_match[] = {
  165. { .compatible = "mediatek,mt7621-wdt" },
  166. {},
  167. };
  168. MODULE_DEVICE_TABLE(of, mt7621_wdt_match);
  169. static struct platform_driver mt7621_wdt_driver = {
  170. .probe = mt7621_wdt_probe,
  171. .shutdown = mt7621_wdt_shutdown,
  172. .driver = {
  173. .name = KBUILD_MODNAME,
  174. .of_match_table = mt7621_wdt_match,
  175. },
  176. };
  177. module_platform_driver(mt7621_wdt_driver);
  178. MODULE_DESCRIPTION("MediaTek MT762x hardware watchdog driver");
  179. MODULE_AUTHOR("John Crispin <john@phrozen.org");
  180. MODULE_LICENSE("GPL v2");