davinci_wdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/mod_devicetable.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <linux/device.h>
  22. #include <linux/clk.h>
  23. #include <linux/err.h>
  24. #define MODULE_NAME "DAVINCI-WDT: "
  25. #define DEFAULT_HEARTBEAT 60
  26. #define MAX_HEARTBEAT 600 /* really the max margin is 264/27MHz*/
  27. /* Timer register set definition */
  28. #define PID12 (0x0)
  29. #define EMUMGT (0x4)
  30. #define TIM12 (0x10)
  31. #define TIM34 (0x14)
  32. #define PRD12 (0x18)
  33. #define PRD34 (0x1C)
  34. #define TCR (0x20)
  35. #define TGCR (0x24)
  36. #define WDTCR (0x28)
  37. /* TCR bit definitions */
  38. #define ENAMODE12_DISABLED (0 << 6)
  39. #define ENAMODE12_ONESHOT (1 << 6)
  40. #define ENAMODE12_PERIODIC (2 << 6)
  41. /* TGCR bit definitions */
  42. #define TIM12RS_UNRESET (1 << 0)
  43. #define TIM34RS_UNRESET (1 << 1)
  44. #define TIMMODE_64BIT_WDOG (2 << 2)
  45. /* WDTCR bit definitions */
  46. #define WDEN (1 << 14)
  47. #define WDFLAG (1 << 15)
  48. #define WDKEY_SEQ0 (0xa5c6 << 16)
  49. #define WDKEY_SEQ1 (0xda7e << 16)
  50. static int heartbeat;
  51. /*
  52. * struct to hold data for each WDT device
  53. * @base - base io address of WD device
  54. * @clk - source clock of WDT
  55. * @wdd - hold watchdog device as is in WDT core
  56. */
  57. struct davinci_wdt_device {
  58. void __iomem *base;
  59. struct clk *clk;
  60. struct watchdog_device wdd;
  61. };
  62. static int davinci_wdt_start(struct watchdog_device *wdd)
  63. {
  64. u32 tgcr;
  65. u32 timer_margin;
  66. unsigned long wdt_freq;
  67. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  68. wdt_freq = clk_get_rate(davinci_wdt->clk);
  69. /* disable, internal clock source */
  70. iowrite32(0, davinci_wdt->base + TCR);
  71. /* reset timer, set mode to 64-bit watchdog, and unreset */
  72. iowrite32(0, davinci_wdt->base + TGCR);
  73. tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
  74. iowrite32(tgcr, davinci_wdt->base + TGCR);
  75. /* clear counter regs */
  76. iowrite32(0, davinci_wdt->base + TIM12);
  77. iowrite32(0, davinci_wdt->base + TIM34);
  78. /* set timeout period */
  79. timer_margin = (((u64)wdd->timeout * wdt_freq) & 0xffffffff);
  80. iowrite32(timer_margin, davinci_wdt->base + PRD12);
  81. timer_margin = (((u64)wdd->timeout * wdt_freq) >> 32);
  82. iowrite32(timer_margin, davinci_wdt->base + PRD34);
  83. /* enable run continuously */
  84. iowrite32(ENAMODE12_PERIODIC, davinci_wdt->base + TCR);
  85. /* Once the WDT is in pre-active state write to
  86. * TIM12, TIM34, PRD12, PRD34, TCR, TGCR, WDTCR are
  87. * write protected (except for the WDKEY field)
  88. */
  89. /* put watchdog in pre-active state */
  90. iowrite32(WDKEY_SEQ0 | WDEN, davinci_wdt->base + WDTCR);
  91. /* put watchdog in active state */
  92. iowrite32(WDKEY_SEQ1 | WDEN, davinci_wdt->base + WDTCR);
  93. return 0;
  94. }
  95. static int davinci_wdt_ping(struct watchdog_device *wdd)
  96. {
  97. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  98. /* put watchdog in service state */
  99. iowrite32(WDKEY_SEQ0, davinci_wdt->base + WDTCR);
  100. /* put watchdog in active state */
  101. iowrite32(WDKEY_SEQ1, davinci_wdt->base + WDTCR);
  102. return 0;
  103. }
  104. static unsigned int davinci_wdt_get_timeleft(struct watchdog_device *wdd)
  105. {
  106. u64 timer_counter;
  107. unsigned long freq;
  108. u32 val;
  109. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  110. /* if timeout has occured then return 0 */
  111. val = ioread32(davinci_wdt->base + WDTCR);
  112. if (val & WDFLAG)
  113. return 0;
  114. freq = clk_get_rate(davinci_wdt->clk);
  115. if (!freq)
  116. return 0;
  117. timer_counter = ioread32(davinci_wdt->base + TIM12);
  118. timer_counter |= ((u64)ioread32(davinci_wdt->base + TIM34) << 32);
  119. do_div(timer_counter, freq);
  120. return wdd->timeout - timer_counter;
  121. }
  122. static int davinci_wdt_restart(struct watchdog_device *wdd,
  123. unsigned long action, void *data)
  124. {
  125. struct davinci_wdt_device *davinci_wdt = watchdog_get_drvdata(wdd);
  126. u32 tgcr, wdtcr;
  127. /* disable, internal clock source */
  128. iowrite32(0, davinci_wdt->base + TCR);
  129. /* reset timer, set mode to 64-bit watchdog, and unreset */
  130. tgcr = 0;
  131. iowrite32(tgcr, davinci_wdt->base + TGCR);
  132. tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
  133. iowrite32(tgcr, davinci_wdt->base + TGCR);
  134. /* clear counter and period regs */
  135. iowrite32(0, davinci_wdt->base + TIM12);
  136. iowrite32(0, davinci_wdt->base + TIM34);
  137. iowrite32(0, davinci_wdt->base + PRD12);
  138. iowrite32(0, davinci_wdt->base + PRD34);
  139. /* put watchdog in pre-active state */
  140. wdtcr = WDKEY_SEQ0 | WDEN;
  141. iowrite32(wdtcr, davinci_wdt->base + WDTCR);
  142. /* put watchdog in active state */
  143. wdtcr = WDKEY_SEQ1 | WDEN;
  144. iowrite32(wdtcr, davinci_wdt->base + WDTCR);
  145. /* write an invalid value to the WDKEY field to trigger a restart */
  146. wdtcr = 0x00004000;
  147. iowrite32(wdtcr, davinci_wdt->base + WDTCR);
  148. return 0;
  149. }
  150. static const struct watchdog_info davinci_wdt_info = {
  151. .options = WDIOF_KEEPALIVEPING,
  152. .identity = "DaVinci/Keystone Watchdog",
  153. };
  154. static const struct watchdog_ops davinci_wdt_ops = {
  155. .owner = THIS_MODULE,
  156. .start = davinci_wdt_start,
  157. .stop = davinci_wdt_ping,
  158. .ping = davinci_wdt_ping,
  159. .get_timeleft = davinci_wdt_get_timeleft,
  160. .restart = davinci_wdt_restart,
  161. };
  162. static int davinci_wdt_probe(struct platform_device *pdev)
  163. {
  164. int ret = 0;
  165. struct device *dev = &pdev->dev;
  166. struct resource *wdt_mem;
  167. struct watchdog_device *wdd;
  168. struct davinci_wdt_device *davinci_wdt;
  169. davinci_wdt = devm_kzalloc(dev, sizeof(*davinci_wdt), GFP_KERNEL);
  170. if (!davinci_wdt)
  171. return -ENOMEM;
  172. davinci_wdt->clk = devm_clk_get(dev, NULL);
  173. if (IS_ERR(davinci_wdt->clk)) {
  174. if (PTR_ERR(davinci_wdt->clk) != -EPROBE_DEFER)
  175. dev_err(&pdev->dev, "failed to get clock node\n");
  176. return PTR_ERR(davinci_wdt->clk);
  177. }
  178. ret = clk_prepare_enable(davinci_wdt->clk);
  179. if (ret) {
  180. dev_err(&pdev->dev, "failed to prepare clock\n");
  181. return ret;
  182. }
  183. platform_set_drvdata(pdev, davinci_wdt);
  184. wdd = &davinci_wdt->wdd;
  185. wdd->info = &davinci_wdt_info;
  186. wdd->ops = &davinci_wdt_ops;
  187. wdd->min_timeout = 1;
  188. wdd->max_timeout = MAX_HEARTBEAT;
  189. wdd->timeout = DEFAULT_HEARTBEAT;
  190. wdd->parent = &pdev->dev;
  191. watchdog_init_timeout(wdd, heartbeat, dev);
  192. dev_info(dev, "heartbeat %d sec\n", wdd->timeout);
  193. watchdog_set_drvdata(wdd, davinci_wdt);
  194. watchdog_set_nowayout(wdd, 1);
  195. watchdog_set_restart_priority(wdd, 128);
  196. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  197. davinci_wdt->base = devm_ioremap_resource(dev, wdt_mem);
  198. if (IS_ERR(davinci_wdt->base)) {
  199. ret = PTR_ERR(davinci_wdt->base);
  200. goto err_clk_disable;
  201. }
  202. ret = watchdog_register_device(wdd);
  203. if (ret) {
  204. dev_err(dev, "cannot register watchdog device\n");
  205. goto err_clk_disable;
  206. }
  207. return 0;
  208. err_clk_disable:
  209. clk_disable_unprepare(davinci_wdt->clk);
  210. return ret;
  211. }
  212. static int davinci_wdt_remove(struct platform_device *pdev)
  213. {
  214. struct davinci_wdt_device *davinci_wdt = platform_get_drvdata(pdev);
  215. watchdog_unregister_device(&davinci_wdt->wdd);
  216. clk_disable_unprepare(davinci_wdt->clk);
  217. return 0;
  218. }
  219. static const struct of_device_id davinci_wdt_of_match[] = {
  220. { .compatible = "ti,davinci-wdt", },
  221. {},
  222. };
  223. MODULE_DEVICE_TABLE(of, davinci_wdt_of_match);
  224. static struct platform_driver platform_wdt_driver = {
  225. .driver = {
  226. .name = "davinci-wdt",
  227. .of_match_table = davinci_wdt_of_match,
  228. },
  229. .probe = davinci_wdt_probe,
  230. .remove = davinci_wdt_remove,
  231. };
  232. module_platform_driver(platform_wdt_driver);
  233. MODULE_AUTHOR("Texas Instruments");
  234. MODULE_DESCRIPTION("DaVinci Watchdog Driver");
  235. module_param(heartbeat, int, 0);
  236. MODULE_PARM_DESC(heartbeat,
  237. "Watchdog heartbeat period in seconds from 1 to "
  238. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  239. __MODULE_STRING(DEFAULT_HEARTBEAT));
  240. MODULE_LICENSE("GPL");
  241. MODULE_ALIAS("platform:davinci-wdt");