rt2880_wdt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Ralink RT288x/RT3xxx/MT76xx built-in hardware watchdog timer
  3. *
  4. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  5. * Copyright (C) 2013 John Crispin <john@phrozen.org>
  6. *
  7. * This driver was based on: drivers/watchdog/softdog.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/reset.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/watchdog.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <asm/mach-ralink/ralink_regs.h>
  22. #define SYSC_RSTSTAT 0x38
  23. #define WDT_RST_CAUSE BIT(1)
  24. #define RALINK_WDT_TIMEOUT 30
  25. #define RALINK_WDT_PRESCALE 65536
  26. #define TIMER_REG_TMR1LOAD 0x00
  27. #define TIMER_REG_TMR1CTL 0x08
  28. #define TMRSTAT_TMR1RST BIT(5)
  29. #define TMR1CTL_ENABLE BIT(7)
  30. #define TMR1CTL_MODE_SHIFT 4
  31. #define TMR1CTL_MODE_MASK 0x3
  32. #define TMR1CTL_MODE_FREE_RUNNING 0x0
  33. #define TMR1CTL_MODE_PERIODIC 0x1
  34. #define TMR1CTL_MODE_TIMEOUT 0x2
  35. #define TMR1CTL_MODE_WDT 0x3
  36. #define TMR1CTL_PRESCALE_MASK 0xf
  37. #define TMR1CTL_PRESCALE_65536 0xf
  38. static struct clk *rt288x_wdt_clk;
  39. static unsigned long rt288x_wdt_freq;
  40. static void __iomem *rt288x_wdt_base;
  41. static struct reset_control *rt288x_wdt_reset;
  42. static bool nowayout = WATCHDOG_NOWAYOUT;
  43. module_param(nowayout, bool, 0);
  44. MODULE_PARM_DESC(nowayout,
  45. "Watchdog cannot be stopped once started (default="
  46. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  47. static inline void rt_wdt_w32(unsigned reg, u32 val)
  48. {
  49. iowrite32(val, rt288x_wdt_base + reg);
  50. }
  51. static inline u32 rt_wdt_r32(unsigned reg)
  52. {
  53. return ioread32(rt288x_wdt_base + reg);
  54. }
  55. static int rt288x_wdt_ping(struct watchdog_device *w)
  56. {
  57. rt_wdt_w32(TIMER_REG_TMR1LOAD, w->timeout * rt288x_wdt_freq);
  58. return 0;
  59. }
  60. static int rt288x_wdt_start(struct watchdog_device *w)
  61. {
  62. u32 t;
  63. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  64. t &= ~(TMR1CTL_MODE_MASK << TMR1CTL_MODE_SHIFT |
  65. TMR1CTL_PRESCALE_MASK);
  66. t |= (TMR1CTL_MODE_WDT << TMR1CTL_MODE_SHIFT |
  67. TMR1CTL_PRESCALE_65536);
  68. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  69. rt288x_wdt_ping(w);
  70. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  71. t |= TMR1CTL_ENABLE;
  72. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  73. return 0;
  74. }
  75. static int rt288x_wdt_stop(struct watchdog_device *w)
  76. {
  77. u32 t;
  78. rt288x_wdt_ping(w);
  79. t = rt_wdt_r32(TIMER_REG_TMR1CTL);
  80. t &= ~TMR1CTL_ENABLE;
  81. rt_wdt_w32(TIMER_REG_TMR1CTL, t);
  82. return 0;
  83. }
  84. static int rt288x_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
  85. {
  86. w->timeout = t;
  87. rt288x_wdt_ping(w);
  88. return 0;
  89. }
  90. static int rt288x_wdt_bootcause(void)
  91. {
  92. if (rt_sysc_r32(SYSC_RSTSTAT) & WDT_RST_CAUSE)
  93. return WDIOF_CARDRESET;
  94. return 0;
  95. }
  96. static const struct watchdog_info rt288x_wdt_info = {
  97. .identity = "Ralink Watchdog",
  98. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  99. };
  100. static const struct watchdog_ops rt288x_wdt_ops = {
  101. .owner = THIS_MODULE,
  102. .start = rt288x_wdt_start,
  103. .stop = rt288x_wdt_stop,
  104. .ping = rt288x_wdt_ping,
  105. .set_timeout = rt288x_wdt_set_timeout,
  106. };
  107. static struct watchdog_device rt288x_wdt_dev = {
  108. .info = &rt288x_wdt_info,
  109. .ops = &rt288x_wdt_ops,
  110. .min_timeout = 1,
  111. };
  112. static int rt288x_wdt_probe(struct platform_device *pdev)
  113. {
  114. struct resource *res;
  115. int ret;
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. rt288x_wdt_base = devm_ioremap_resource(&pdev->dev, res);
  118. if (IS_ERR(rt288x_wdt_base))
  119. return PTR_ERR(rt288x_wdt_base);
  120. rt288x_wdt_clk = devm_clk_get(&pdev->dev, NULL);
  121. if (IS_ERR(rt288x_wdt_clk))
  122. return PTR_ERR(rt288x_wdt_clk);
  123. rt288x_wdt_reset = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  124. if (!IS_ERR(rt288x_wdt_reset))
  125. reset_control_deassert(rt288x_wdt_reset);
  126. rt288x_wdt_freq = clk_get_rate(rt288x_wdt_clk) / RALINK_WDT_PRESCALE;
  127. rt288x_wdt_dev.bootstatus = rt288x_wdt_bootcause();
  128. rt288x_wdt_dev.max_timeout = (0xfffful / rt288x_wdt_freq);
  129. rt288x_wdt_dev.parent = &pdev->dev;
  130. watchdog_init_timeout(&rt288x_wdt_dev, rt288x_wdt_dev.max_timeout,
  131. &pdev->dev);
  132. watchdog_set_nowayout(&rt288x_wdt_dev, nowayout);
  133. ret = watchdog_register_device(&rt288x_wdt_dev);
  134. if (!ret)
  135. dev_info(&pdev->dev, "Initialized\n");
  136. return 0;
  137. }
  138. static int rt288x_wdt_remove(struct platform_device *pdev)
  139. {
  140. watchdog_unregister_device(&rt288x_wdt_dev);
  141. return 0;
  142. }
  143. static void rt288x_wdt_shutdown(struct platform_device *pdev)
  144. {
  145. rt288x_wdt_stop(&rt288x_wdt_dev);
  146. }
  147. static const struct of_device_id rt288x_wdt_match[] = {
  148. { .compatible = "ralink,rt2880-wdt" },
  149. {},
  150. };
  151. MODULE_DEVICE_TABLE(of, rt288x_wdt_match);
  152. static struct platform_driver rt288x_wdt_driver = {
  153. .probe = rt288x_wdt_probe,
  154. .remove = rt288x_wdt_remove,
  155. .shutdown = rt288x_wdt_shutdown,
  156. .driver = {
  157. .name = KBUILD_MODNAME,
  158. .of_match_table = rt288x_wdt_match,
  159. },
  160. };
  161. module_platform_driver(rt288x_wdt_driver);
  162. MODULE_DESCRIPTION("MediaTek/Ralink RT288x/RT3xxx hardware watchdog driver");
  163. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
  164. MODULE_LICENSE("GPL v2");