sirfsoc_wdt.c 5.7 KB

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