sirfsoc_wdt.c 5.6 KB

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