mt7621_wdt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Ralink MT7621/MT7628 built-in hardware watchdog timer
  3. *
  4. * Copyright (C) 2014 John Crispin <john@phrozen.org>
  5. *
  6. * This driver was based on: drivers/watchdog/rt2880_wdt.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/reset.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <asm/mach-ralink/ralink_regs.h>
  21. #define SYSC_RSTSTAT 0x38
  22. #define WDT_RST_CAUSE BIT(1)
  23. #define RALINK_WDT_TIMEOUT 30
  24. #define TIMER_REG_TMRSTAT 0x00
  25. #define TIMER_REG_TMR1LOAD 0x24
  26. #define TIMER_REG_TMR1CTL 0x20
  27. #define TMR1CTL_ENABLE BIT(7)
  28. #define TMR1CTL_RESTART BIT(9)
  29. #define TMR1CTL_PRESCALE_SHIFT 16
  30. static void __iomem *mt7621_wdt_base;
  31. static struct reset_control *mt7621_wdt_reset;
  32. static bool nowayout = WATCHDOG_NOWAYOUT;
  33. module_param(nowayout, bool, 0);
  34. MODULE_PARM_DESC(nowayout,
  35. "Watchdog cannot be stopped once started (default="
  36. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  37. static inline void rt_wdt_w32(unsigned reg, u32 val)
  38. {
  39. iowrite32(val, mt7621_wdt_base + reg);
  40. }
  41. static inline u32 rt_wdt_r32(unsigned reg)
  42. {
  43. return ioread32(mt7621_wdt_base + reg);
  44. }
  45. static int mt7621_wdt_ping(struct watchdog_device *w)
  46. {
  47. rt_wdt_w32(TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
  48. return 0;
  49. }
  50. static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
  51. {
  52. w->timeout = t;
  53. rt_wdt_w32(TIMER_REG_TMR1LOAD, t * 1000);
  54. mt7621_wdt_ping(w);
  55. return 0;
  56. }
  57. static int mt7621_wdt_start(struct watchdog_device *w)
  58. {
  59. u32 t;
  60. /* set the prescaler to 1ms == 1000us */
  61. rt_wdt_w32(TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
  62. mt7621_wdt_set_timeout(w, w->timeout);
  63. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  64. t |= TMR1CTL_ENABLE;
  65. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  66. return 0;
  67. }
  68. static int mt7621_wdt_stop(struct watchdog_device *w)
  69. {
  70. u32 t;
  71. mt7621_wdt_ping(w);
  72. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  73. t &= ~TMR1CTL_ENABLE;
  74. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  75. return 0;
  76. }
  77. static int mt7621_wdt_bootcause(void)
  78. {
  79. if (rt_sysc_r32(SYSC_RSTSTAT) & WDT_RST_CAUSE)
  80. return WDIOF_CARDRESET;
  81. return 0;
  82. }
  83. static int mt7621_wdt_is_running(struct watchdog_device *w)
  84. {
  85. return !!(rt_wdt_r32(TIMER_REG_TMR1CTL) & TMR1CTL_ENABLE);
  86. }
  87. static const struct watchdog_info mt7621_wdt_info = {
  88. .identity = "Mediatek Watchdog",
  89. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  90. };
  91. static const struct watchdog_ops mt7621_wdt_ops = {
  92. .owner = THIS_MODULE,
  93. .start = mt7621_wdt_start,
  94. .stop = mt7621_wdt_stop,
  95. .ping = mt7621_wdt_ping,
  96. .set_timeout = mt7621_wdt_set_timeout,
  97. };
  98. static struct watchdog_device mt7621_wdt_dev = {
  99. .info = &mt7621_wdt_info,
  100. .ops = &mt7621_wdt_ops,
  101. .min_timeout = 1,
  102. .max_timeout = 0xfffful / 1000,
  103. };
  104. static int mt7621_wdt_probe(struct platform_device *pdev)
  105. {
  106. struct resource *res;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. mt7621_wdt_base = devm_ioremap_resource(&pdev->dev, res);
  109. if (IS_ERR(mt7621_wdt_base))
  110. return PTR_ERR(mt7621_wdt_base);
  111. mt7621_wdt_reset = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  112. if (!IS_ERR(mt7621_wdt_reset))
  113. reset_control_deassert(mt7621_wdt_reset);
  114. mt7621_wdt_dev.bootstatus = mt7621_wdt_bootcause();
  115. watchdog_init_timeout(&mt7621_wdt_dev, mt7621_wdt_dev.max_timeout,
  116. &pdev->dev);
  117. watchdog_set_nowayout(&mt7621_wdt_dev, nowayout);
  118. if (mt7621_wdt_is_running(&mt7621_wdt_dev)) {
  119. /*
  120. * Make sure to apply timeout from watchdog core, taking
  121. * the prescaler of this driver here into account (the
  122. * boot loader might be using a different prescaler).
  123. *
  124. * To avoid spurious resets because of different scaling,
  125. * we first disable the watchdog, set the new prescaler
  126. * and timeout, and then re-enable the watchdog.
  127. */
  128. mt7621_wdt_stop(&mt7621_wdt_dev);
  129. mt7621_wdt_start(&mt7621_wdt_dev);
  130. set_bit(WDOG_HW_RUNNING, &mt7621_wdt_dev.status);
  131. }
  132. return devm_watchdog_register_device(&pdev->dev, &mt7621_wdt_dev);
  133. }
  134. static void mt7621_wdt_shutdown(struct platform_device *pdev)
  135. {
  136. mt7621_wdt_stop(&mt7621_wdt_dev);
  137. }
  138. static const struct of_device_id mt7621_wdt_match[] = {
  139. { .compatible = "mediatek,mt7621-wdt" },
  140. {},
  141. };
  142. MODULE_DEVICE_TABLE(of, mt7621_wdt_match);
  143. static struct platform_driver mt7621_wdt_driver = {
  144. .probe = mt7621_wdt_probe,
  145. .shutdown = mt7621_wdt_shutdown,
  146. .driver = {
  147. .name = KBUILD_MODNAME,
  148. .of_match_table = mt7621_wdt_match,
  149. },
  150. };
  151. module_platform_driver(mt7621_wdt_driver);
  152. MODULE_DESCRIPTION("MediaTek MT762x hardware watchdog driver");
  153. MODULE_AUTHOR("John Crispin <john@phrozen.org");
  154. MODULE_LICENSE("GPL v2");