davinci_wdt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * drivers/char/watchdog/davinci_wdt.c
  3. *
  4. * Watchdog driver for DaVinci DM644x/DM646x processors
  5. *
  6. * Copyright (C) 2006-2013 Texas Instruments.
  7. *
  8. * 2007 (c) MontaVista Software, Inc. This file is licensed under
  9. * the terms of the GNU General Public License version 2. This program
  10. * is licensed "as is" without any warranty of any kind, whether express
  11. * or implied.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/watchdog.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/device.h>
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #define MODULE_NAME "DAVINCI-WDT: "
  24. #define DEFAULT_HEARTBEAT 60
  25. #define MAX_HEARTBEAT 600 /* really the max margin is 264/27MHz*/
  26. /* Timer register set definition */
  27. #define PID12 (0x0)
  28. #define EMUMGT (0x4)
  29. #define TIM12 (0x10)
  30. #define TIM34 (0x14)
  31. #define PRD12 (0x18)
  32. #define PRD34 (0x1C)
  33. #define TCR (0x20)
  34. #define TGCR (0x24)
  35. #define WDTCR (0x28)
  36. /* TCR bit definitions */
  37. #define ENAMODE12_DISABLED (0 << 6)
  38. #define ENAMODE12_ONESHOT (1 << 6)
  39. #define ENAMODE12_PERIODIC (2 << 6)
  40. /* TGCR bit definitions */
  41. #define TIM12RS_UNRESET (1 << 0)
  42. #define TIM34RS_UNRESET (1 << 1)
  43. #define TIMMODE_64BIT_WDOG (2 << 2)
  44. /* WDTCR bit definitions */
  45. #define WDEN (1 << 14)
  46. #define WDFLAG (1 << 15)
  47. #define WDKEY_SEQ0 (0xa5c6 << 16)
  48. #define WDKEY_SEQ1 (0xda7e << 16)
  49. static int heartbeat;
  50. /*
  51. * struct to hold data for each WDT device
  52. * @base - base io address of WD device
  53. * @clk - source clock of WDT
  54. * @wdd - hold watchdog device as is in WDT core
  55. */
  56. struct davinci_wdt_device {
  57. void __iomem *base;
  58. struct clk *clk;
  59. struct watchdog_device wdd;
  60. };
  61. static int davinci_wdt_start(struct watchdog_device *wdd)
  62. {
  63. u32 tgcr;
  64. u32 timer_margin;
  65. unsigned long wdt_freq;
  66. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  67. wdt_freq = clk_get_rate(davinci_wdt->clk);
  68. /* disable, internal clock source */
  69. iowrite32(0, davinci_wdt->base + TCR);
  70. /* reset timer, set mode to 64-bit watchdog, and unreset */
  71. iowrite32(0, davinci_wdt->base + TGCR);
  72. tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
  73. iowrite32(tgcr, davinci_wdt->base + TGCR);
  74. /* clear counter regs */
  75. iowrite32(0, davinci_wdt->base + TIM12);
  76. iowrite32(0, davinci_wdt->base + TIM34);
  77. /* set timeout period */
  78. timer_margin = (((u64)wdd->timeout * wdt_freq) & 0xffffffff);
  79. iowrite32(timer_margin, davinci_wdt->base + PRD12);
  80. timer_margin = (((u64)wdd->timeout * wdt_freq) >> 32);
  81. iowrite32(timer_margin, davinci_wdt->base + PRD34);
  82. /* enable run continuously */
  83. iowrite32(ENAMODE12_PERIODIC, davinci_wdt->base + TCR);
  84. /* Once the WDT is in pre-active state write to
  85. * TIM12, TIM34, PRD12, PRD34, TCR, TGCR, WDTCR are
  86. * write protected (except for the WDKEY field)
  87. */
  88. /* put watchdog in pre-active state */
  89. iowrite32(WDKEY_SEQ0 | WDEN, davinci_wdt->base + WDTCR);
  90. /* put watchdog in active state */
  91. iowrite32(WDKEY_SEQ1 | WDEN, davinci_wdt->base + WDTCR);
  92. return 0;
  93. }
  94. static int davinci_wdt_ping(struct watchdog_device *wdd)
  95. {
  96. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  97. /* put watchdog in service state */
  98. iowrite32(WDKEY_SEQ0, davinci_wdt->base + WDTCR);
  99. /* put watchdog in active state */
  100. iowrite32(WDKEY_SEQ1, davinci_wdt->base + WDTCR);
  101. return 0;
  102. }
  103. static unsigned int davinci_wdt_get_timeleft(struct watchdog_device *wdd)
  104. {
  105. u64 timer_counter;
  106. unsigned long freq;
  107. u32 val;
  108. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  109. /* if timeout has occured then return 0 */
  110. val = ioread32(davinci_wdt->base + WDTCR);
  111. if (val & WDFLAG)
  112. return 0;
  113. freq = clk_get_rate(davinci_wdt->clk);
  114. if (!freq)
  115. return 0;
  116. timer_counter = ioread32(davinci_wdt->base + TIM12);
  117. timer_counter |= ((u64)ioread32(davinci_wdt->base + TIM34) << 32);
  118. do_div(timer_counter, freq);
  119. return wdd->timeout - timer_counter;
  120. }
  121. static const struct watchdog_info davinci_wdt_info = {
  122. .options = WDIOF_KEEPALIVEPING,
  123. .identity = "DaVinci/Keystone Watchdog",
  124. };
  125. static const struct watchdog_ops davinci_wdt_ops = {
  126. .owner = THIS_MODULE,
  127. .start = davinci_wdt_start,
  128. .stop = davinci_wdt_ping,
  129. .ping = davinci_wdt_ping,
  130. .get_timeleft = davinci_wdt_get_timeleft,
  131. };
  132. static int davinci_wdt_probe(struct platform_device *pdev)
  133. {
  134. int ret = 0;
  135. struct device *dev = &pdev->dev;
  136. struct resource *wdt_mem;
  137. struct watchdog_device *wdd;
  138. struct davinci_wdt_device *davinci_wdt;
  139. davinci_wdt = devm_kzalloc(dev, sizeof(*davinci_wdt), GFP_KERNEL);
  140. if (!davinci_wdt)
  141. return -ENOMEM;
  142. davinci_wdt->clk = devm_clk_get(dev, NULL);
  143. if (WARN_ON(IS_ERR(davinci_wdt->clk)))
  144. return PTR_ERR(davinci_wdt->clk);
  145. clk_prepare_enable(davinci_wdt->clk);
  146. platform_set_drvdata(pdev, davinci_wdt);
  147. wdd = &davinci_wdt->wdd;
  148. wdd->info = &davinci_wdt_info;
  149. wdd->ops = &davinci_wdt_ops;
  150. wdd->min_timeout = 1;
  151. wdd->max_timeout = MAX_HEARTBEAT;
  152. wdd->timeout = DEFAULT_HEARTBEAT;
  153. wdd->parent = &pdev->dev;
  154. watchdog_init_timeout(wdd, heartbeat, dev);
  155. dev_info(dev, "heartbeat %d sec\n", wdd->timeout);
  156. watchdog_set_drvdata(wdd, davinci_wdt);
  157. watchdog_set_nowayout(wdd, 1);
  158. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  159. davinci_wdt->base = devm_ioremap_resource(dev, wdt_mem);
  160. if (IS_ERR(davinci_wdt->base))
  161. return PTR_ERR(davinci_wdt->base);
  162. ret = watchdog_register_device(wdd);
  163. if (ret < 0)
  164. dev_err(dev, "cannot register watchdog device\n");
  165. return ret;
  166. }
  167. static int davinci_wdt_remove(struct platform_device *pdev)
  168. {
  169. struct davinci_wdt_device *davinci_wdt = platform_get_drvdata(pdev);
  170. watchdog_unregister_device(&davinci_wdt->wdd);
  171. clk_disable_unprepare(davinci_wdt->clk);
  172. return 0;
  173. }
  174. static const struct of_device_id davinci_wdt_of_match[] = {
  175. { .compatible = "ti,davinci-wdt", },
  176. {},
  177. };
  178. MODULE_DEVICE_TABLE(of, davinci_wdt_of_match);
  179. static struct platform_driver platform_wdt_driver = {
  180. .driver = {
  181. .name = "davinci-wdt",
  182. .of_match_table = davinci_wdt_of_match,
  183. },
  184. .probe = davinci_wdt_probe,
  185. .remove = davinci_wdt_remove,
  186. };
  187. module_platform_driver(platform_wdt_driver);
  188. MODULE_AUTHOR("Texas Instruments");
  189. MODULE_DESCRIPTION("DaVinci Watchdog Driver");
  190. module_param(heartbeat, int, 0);
  191. MODULE_PARM_DESC(heartbeat,
  192. "Watchdog heartbeat period in seconds from 1 to "
  193. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  194. __MODULE_STRING(DEFAULT_HEARTBEAT));
  195. MODULE_LICENSE("GPL");
  196. MODULE_ALIAS("platform:davinci-wdt");