sirfsoc_wdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Watchdog driver for CSR SiRFprimaII and SiRFatlasVI
  3. *
  4. * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/watchdog.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/of.h>
  13. #include <linux/io.h>
  14. #include <linux/uaccess.h>
  15. #define SIRFSOC_TIMER_COUNTER_LO 0x0000
  16. #define SIRFSOC_TIMER_MATCH_0 0x0008
  17. #define SIRFSOC_TIMER_INT_EN 0x0024
  18. #define SIRFSOC_TIMER_WATCHDOG_EN 0x0028
  19. #define SIRFSOC_TIMER_LATCH 0x0030
  20. #define SIRFSOC_TIMER_LATCHED_LO 0x0034
  21. #define SIRFSOC_TIMER_WDT_INDEX 5
  22. #define SIRFSOC_WDT_MIN_TIMEOUT 30 /* 30 secs */
  23. #define SIRFSOC_WDT_MAX_TIMEOUT (10 * 60) /* 10 mins */
  24. #define SIRFSOC_WDT_DEFAULT_TIMEOUT 30 /* 30 secs */
  25. static unsigned int timeout = SIRFSOC_WDT_DEFAULT_TIMEOUT;
  26. static bool nowayout = WATCHDOG_NOWAYOUT;
  27. module_param(timeout, uint, 0);
  28. module_param(nowayout, bool, 0);
  29. MODULE_PARM_DESC(timeout, "Default watchdog timeout (in seconds)");
  30. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  31. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. static unsigned int sirfsoc_wdt_gettimeleft(struct watchdog_device *wdd)
  33. {
  34. u32 counter, match;
  35. void __iomem *wdt_base;
  36. int time_left;
  37. wdt_base = watchdog_get_drvdata(wdd);
  38. counter = readl(wdt_base + SIRFSOC_TIMER_COUNTER_LO);
  39. match = readl(wdt_base +
  40. SIRFSOC_TIMER_MATCH_0 + (SIRFSOC_TIMER_WDT_INDEX << 2));
  41. time_left = match - counter;
  42. return time_left / CLOCK_TICK_RATE;
  43. }
  44. static int sirfsoc_wdt_updatetimeout(struct watchdog_device *wdd)
  45. {
  46. u32 counter, timeout_ticks;
  47. void __iomem *wdt_base;
  48. timeout_ticks = wdd->timeout * CLOCK_TICK_RATE;
  49. wdt_base = watchdog_get_drvdata(wdd);
  50. /* Enable the latch before reading the LATCH_LO register */
  51. writel(1, wdt_base + SIRFSOC_TIMER_LATCH);
  52. /* Set the TO value */
  53. counter = readl(wdt_base + SIRFSOC_TIMER_LATCHED_LO);
  54. counter += timeout_ticks;
  55. writel(counter, wdt_base +
  56. SIRFSOC_TIMER_MATCH_0 + (SIRFSOC_TIMER_WDT_INDEX << 2));
  57. return 0;
  58. }
  59. static int sirfsoc_wdt_enable(struct watchdog_device *wdd)
  60. {
  61. void __iomem *wdt_base = watchdog_get_drvdata(wdd);
  62. sirfsoc_wdt_updatetimeout(wdd);
  63. /*
  64. * NOTE: If interrupt is not enabled
  65. * then WD-Reset doesn't get generated at all.
  66. */
  67. writel(readl(wdt_base + SIRFSOC_TIMER_INT_EN)
  68. | (1 << SIRFSOC_TIMER_WDT_INDEX),
  69. wdt_base + SIRFSOC_TIMER_INT_EN);
  70. writel(1, wdt_base + SIRFSOC_TIMER_WATCHDOG_EN);
  71. return 0;
  72. }
  73. static int sirfsoc_wdt_disable(struct watchdog_device *wdd)
  74. {
  75. void __iomem *wdt_base = watchdog_get_drvdata(wdd);
  76. writel(0, wdt_base + SIRFSOC_TIMER_WATCHDOG_EN);
  77. writel(readl(wdt_base + SIRFSOC_TIMER_INT_EN)
  78. & (~(1 << SIRFSOC_TIMER_WDT_INDEX)),
  79. wdt_base + SIRFSOC_TIMER_INT_EN);
  80. return 0;
  81. }
  82. static int sirfsoc_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
  83. {
  84. wdd->timeout = to;
  85. sirfsoc_wdt_updatetimeout(wdd);
  86. return 0;
  87. }
  88. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  89. static const struct watchdog_info sirfsoc_wdt_ident = {
  90. .options = OPTIONS,
  91. .firmware_version = 0,
  92. .identity = "SiRFSOC Watchdog",
  93. };
  94. static struct watchdog_ops sirfsoc_wdt_ops = {
  95. .owner = THIS_MODULE,
  96. .start = sirfsoc_wdt_enable,
  97. .stop = sirfsoc_wdt_disable,
  98. .get_timeleft = sirfsoc_wdt_gettimeleft,
  99. .ping = sirfsoc_wdt_updatetimeout,
  100. .set_timeout = sirfsoc_wdt_settimeout,
  101. };
  102. static struct watchdog_device sirfsoc_wdd = {
  103. .info = &sirfsoc_wdt_ident,
  104. .ops = &sirfsoc_wdt_ops,
  105. .timeout = SIRFSOC_WDT_DEFAULT_TIMEOUT,
  106. .min_timeout = SIRFSOC_WDT_MIN_TIMEOUT,
  107. .max_timeout = SIRFSOC_WDT_MAX_TIMEOUT,
  108. };
  109. static int sirfsoc_wdt_probe(struct platform_device *pdev)
  110. {
  111. struct resource *res;
  112. int ret;
  113. void __iomem *base;
  114. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  115. base = devm_ioremap_resource(&pdev->dev, res);
  116. if (IS_ERR(base))
  117. return PTR_ERR(base);
  118. watchdog_set_drvdata(&sirfsoc_wdd, base);
  119. watchdog_init_timeout(&sirfsoc_wdd, timeout, &pdev->dev);
  120. watchdog_set_nowayout(&sirfsoc_wdd, nowayout);
  121. ret = watchdog_register_device(&sirfsoc_wdd);
  122. if (ret)
  123. return ret;
  124. platform_set_drvdata(pdev, &sirfsoc_wdd);
  125. return 0;
  126. }
  127. static void sirfsoc_wdt_shutdown(struct platform_device *pdev)
  128. {
  129. struct watchdog_device *wdd = platform_get_drvdata(pdev);
  130. sirfsoc_wdt_disable(wdd);
  131. }
  132. static int sirfsoc_wdt_remove(struct platform_device *pdev)
  133. {
  134. sirfsoc_wdt_shutdown(pdev);
  135. return 0;
  136. }
  137. #ifdef CONFIG_PM_SLEEP
  138. static int sirfsoc_wdt_suspend(struct device *dev)
  139. {
  140. return 0;
  141. }
  142. static int sirfsoc_wdt_resume(struct device *dev)
  143. {
  144. struct watchdog_device *wdd = dev_get_drvdata(dev);
  145. /*
  146. * NOTE: Since timer controller registers settings are saved
  147. * and restored back by the timer-prima2.c, so we need not
  148. * update WD settings except refreshing timeout.
  149. */
  150. sirfsoc_wdt_updatetimeout(wdd);
  151. return 0;
  152. }
  153. #endif
  154. static SIMPLE_DEV_PM_OPS(sirfsoc_wdt_pm_ops,
  155. sirfsoc_wdt_suspend, sirfsoc_wdt_resume);
  156. static const struct of_device_id sirfsoc_wdt_of_match[] = {
  157. { .compatible = "sirf,prima2-tick"},
  158. {},
  159. };
  160. MODULE_DEVICE_TABLE(of, sirfsoc_wdt_of_match);
  161. static struct platform_driver sirfsoc_wdt_driver = {
  162. .driver = {
  163. .name = "sirfsoc-wdt",
  164. .owner = THIS_MODULE,
  165. .pm = &sirfsoc_wdt_pm_ops,
  166. .of_match_table = of_match_ptr(sirfsoc_wdt_of_match),
  167. },
  168. .probe = sirfsoc_wdt_probe,
  169. .remove = sirfsoc_wdt_remove,
  170. .shutdown = sirfsoc_wdt_shutdown,
  171. };
  172. module_platform_driver(sirfsoc_wdt_driver);
  173. MODULE_DESCRIPTION("SiRF SoC watchdog driver");
  174. MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
  175. MODULE_LICENSE("GPL v2");
  176. MODULE_ALIAS("platform:sirfsoc-wdt");