mt7621_wdt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Ralink MT7621/MT7628 built-in hardware watchdog timer
  3. *
  4. * Copyright (C) 2014 John Crispin <blogic@openwrt.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 <asm/mach-ralink/ralink_regs.h>
  20. #define SYSC_RSTSTAT 0x38
  21. #define WDT_RST_CAUSE BIT(1)
  22. #define RALINK_WDT_TIMEOUT 30
  23. #define TIMER_REG_TMRSTAT 0x00
  24. #define TIMER_REG_TMR1LOAD 0x24
  25. #define TIMER_REG_TMR1CTL 0x20
  26. #define TMR1CTL_ENABLE BIT(7)
  27. #define TMR1CTL_RESTART BIT(9)
  28. #define TMR1CTL_PRESCALE_SHIFT 16
  29. static void __iomem *mt7621_wdt_base;
  30. static struct reset_control *mt7621_wdt_reset;
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. module_param(nowayout, bool, 0);
  33. MODULE_PARM_DESC(nowayout,
  34. "Watchdog cannot be stopped once started (default="
  35. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  36. static inline void rt_wdt_w32(unsigned reg, u32 val)
  37. {
  38. iowrite32(val, mt7621_wdt_base + reg);
  39. }
  40. static inline u32 rt_wdt_r32(unsigned reg)
  41. {
  42. return ioread32(mt7621_wdt_base + reg);
  43. }
  44. static int mt7621_wdt_ping(struct watchdog_device *w)
  45. {
  46. rt_wdt_w32(TIMER_REG_TMRSTAT, TMR1CTL_RESTART);
  47. return 0;
  48. }
  49. static int mt7621_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
  50. {
  51. w->timeout = t;
  52. rt_wdt_w32(TIMER_REG_TMR1LOAD, t * 1000);
  53. mt7621_wdt_ping(w);
  54. return 0;
  55. }
  56. static int mt7621_wdt_start(struct watchdog_device *w)
  57. {
  58. u32 t;
  59. /* set the prescaler to 1ms == 1000us */
  60. rt_wdt_w32(TIMER_REG_TMR1CTL, 1000 << TMR1CTL_PRESCALE_SHIFT);
  61. mt7621_wdt_set_timeout(w, w->timeout);
  62. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  63. t |= TMR1CTL_ENABLE;
  64. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  65. return 0;
  66. }
  67. static int mt7621_wdt_stop(struct watchdog_device *w)
  68. {
  69. u32 t;
  70. mt7621_wdt_ping(w);
  71. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  72. t &= ~TMR1CTL_ENABLE;
  73. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  74. return 0;
  75. }
  76. static int mt7621_wdt_bootcause(void)
  77. {
  78. if (rt_sysc_r32(SYSC_RSTSTAT) & WDT_RST_CAUSE)
  79. return WDIOF_CARDRESET;
  80. return 0;
  81. }
  82. static struct watchdog_info mt7621_wdt_info = {
  83. .identity = "Mediatek Watchdog",
  84. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  85. };
  86. static struct watchdog_ops mt7621_wdt_ops = {
  87. .owner = THIS_MODULE,
  88. .start = mt7621_wdt_start,
  89. .stop = mt7621_wdt_stop,
  90. .ping = mt7621_wdt_ping,
  91. .set_timeout = mt7621_wdt_set_timeout,
  92. };
  93. static struct watchdog_device mt7621_wdt_dev = {
  94. .info = &mt7621_wdt_info,
  95. .ops = &mt7621_wdt_ops,
  96. .min_timeout = 1,
  97. .max_timeout = 0xfffful / 1000,
  98. };
  99. static int mt7621_wdt_probe(struct platform_device *pdev)
  100. {
  101. struct resource *res;
  102. int ret;
  103. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  104. mt7621_wdt_base = devm_ioremap_resource(&pdev->dev, res);
  105. if (IS_ERR(mt7621_wdt_base))
  106. return PTR_ERR(mt7621_wdt_base);
  107. mt7621_wdt_reset = devm_reset_control_get(&pdev->dev, NULL);
  108. if (!IS_ERR(mt7621_wdt_reset))
  109. reset_control_deassert(mt7621_wdt_reset);
  110. mt7621_wdt_dev.bootstatus = mt7621_wdt_bootcause();
  111. watchdog_init_timeout(&mt7621_wdt_dev, mt7621_wdt_dev.max_timeout,
  112. &pdev->dev);
  113. watchdog_set_nowayout(&mt7621_wdt_dev, nowayout);
  114. ret = watchdog_register_device(&mt7621_wdt_dev);
  115. return 0;
  116. }
  117. static int mt7621_wdt_remove(struct platform_device *pdev)
  118. {
  119. watchdog_unregister_device(&mt7621_wdt_dev);
  120. return 0;
  121. }
  122. static void mt7621_wdt_shutdown(struct platform_device *pdev)
  123. {
  124. mt7621_wdt_stop(&mt7621_wdt_dev);
  125. }
  126. static const struct of_device_id mt7621_wdt_match[] = {
  127. { .compatible = "mediatek,mt7621-wdt" },
  128. {},
  129. };
  130. MODULE_DEVICE_TABLE(of, mt7621_wdt_match);
  131. static struct platform_driver mt7621_wdt_driver = {
  132. .probe = mt7621_wdt_probe,
  133. .remove = mt7621_wdt_remove,
  134. .shutdown = mt7621_wdt_shutdown,
  135. .driver = {
  136. .name = KBUILD_MODNAME,
  137. .of_match_table = mt7621_wdt_match,
  138. },
  139. };
  140. module_platform_driver(mt7621_wdt_driver);
  141. MODULE_DESCRIPTION("MediaTek MT762x hardware watchdog driver");
  142. MODULE_AUTHOR("John Crispin <blogic@openwrt.org");
  143. MODULE_LICENSE("GPL v2");