davinci_wdt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * drivers/char/watchdog/davinci_wdt.c
  3. *
  4. * Watchdog driver for DaVinci DM644x/DM646x processors
  5. *
  6. * Copyright (C) 2006 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/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/init.h>
  21. #include <linux/bitops.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. #include <linux/device.h>
  27. #include <linux/clk.h>
  28. #include <linux/slab.h>
  29. #define MODULE_NAME "DAVINCI-WDT: "
  30. #define DEFAULT_HEARTBEAT 60
  31. #define MAX_HEARTBEAT 600 /* really the max margin is 264/27MHz*/
  32. /* Timer register set definition */
  33. #define PID12 (0x0)
  34. #define EMUMGT (0x4)
  35. #define TIM12 (0x10)
  36. #define TIM34 (0x14)
  37. #define PRD12 (0x18)
  38. #define PRD34 (0x1C)
  39. #define TCR (0x20)
  40. #define TGCR (0x24)
  41. #define WDTCR (0x28)
  42. /* TCR bit definitions */
  43. #define ENAMODE12_DISABLED (0 << 6)
  44. #define ENAMODE12_ONESHOT (1 << 6)
  45. #define ENAMODE12_PERIODIC (2 << 6)
  46. /* TGCR bit definitions */
  47. #define TIM12RS_UNRESET (1 << 0)
  48. #define TIM34RS_UNRESET (1 << 1)
  49. #define TIMMODE_64BIT_WDOG (2 << 2)
  50. /* WDTCR bit definitions */
  51. #define WDEN (1 << 14)
  52. #define WDFLAG (1 << 15)
  53. #define WDKEY_SEQ0 (0xa5c6 << 16)
  54. #define WDKEY_SEQ1 (0xda7e << 16)
  55. static int heartbeat = DEFAULT_HEARTBEAT;
  56. static DEFINE_SPINLOCK(io_lock);
  57. static unsigned long wdt_status;
  58. #define WDT_IN_USE 0
  59. #define WDT_OK_TO_CLOSE 1
  60. #define WDT_REGION_INITED 2
  61. #define WDT_DEVICE_INITED 3
  62. static struct resource *wdt_mem;
  63. static void __iomem *wdt_base;
  64. struct clk *wdt_clk;
  65. static void wdt_service(void)
  66. {
  67. spin_lock(&io_lock);
  68. /* put watchdog in service state */
  69. iowrite32(WDKEY_SEQ0, wdt_base + WDTCR);
  70. /* put watchdog in active state */
  71. iowrite32(WDKEY_SEQ1, wdt_base + WDTCR);
  72. spin_unlock(&io_lock);
  73. }
  74. static void wdt_enable(void)
  75. {
  76. u32 tgcr;
  77. u32 timer_margin;
  78. unsigned long wdt_freq;
  79. wdt_freq = clk_get_rate(wdt_clk);
  80. spin_lock(&io_lock);
  81. /* disable, internal clock source */
  82. iowrite32(0, wdt_base + TCR);
  83. /* reset timer, set mode to 64-bit watchdog, and unreset */
  84. iowrite32(0, wdt_base + TGCR);
  85. tgcr = TIMMODE_64BIT_WDOG | TIM12RS_UNRESET | TIM34RS_UNRESET;
  86. iowrite32(tgcr, wdt_base + TGCR);
  87. /* clear counter regs */
  88. iowrite32(0, wdt_base + TIM12);
  89. iowrite32(0, wdt_base + TIM34);
  90. /* set timeout period */
  91. timer_margin = (((u64)heartbeat * wdt_freq) & 0xffffffff);
  92. iowrite32(timer_margin, wdt_base + PRD12);
  93. timer_margin = (((u64)heartbeat * wdt_freq) >> 32);
  94. iowrite32(timer_margin, wdt_base + PRD34);
  95. /* enable run continuously */
  96. iowrite32(ENAMODE12_PERIODIC, wdt_base + TCR);
  97. /* Once the WDT is in pre-active state write to
  98. * TIM12, TIM34, PRD12, PRD34, TCR, TGCR, WDTCR are
  99. * write protected (except for the WDKEY field)
  100. */
  101. /* put watchdog in pre-active state */
  102. iowrite32(WDKEY_SEQ0 | WDEN, wdt_base + WDTCR);
  103. /* put watchdog in active state */
  104. iowrite32(WDKEY_SEQ1 | WDEN, wdt_base + WDTCR);
  105. spin_unlock(&io_lock);
  106. }
  107. static int davinci_wdt_open(struct inode *inode, struct file *file)
  108. {
  109. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  110. return -EBUSY;
  111. wdt_enable();
  112. return nonseekable_open(inode, file);
  113. }
  114. static ssize_t
  115. davinci_wdt_write(struct file *file, const char *data, size_t len,
  116. loff_t *ppos)
  117. {
  118. if (len)
  119. wdt_service();
  120. return len;
  121. }
  122. static const struct watchdog_info ident = {
  123. .options = WDIOF_KEEPALIVEPING,
  124. .identity = "DaVinci Watchdog",
  125. };
  126. static long davinci_wdt_ioctl(struct file *file,
  127. unsigned int cmd, unsigned long arg)
  128. {
  129. int ret = -ENOTTY;
  130. switch (cmd) {
  131. case WDIOC_GETSUPPORT:
  132. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  133. sizeof(ident)) ? -EFAULT : 0;
  134. break;
  135. case WDIOC_GETSTATUS:
  136. case WDIOC_GETBOOTSTATUS:
  137. ret = put_user(0, (int *)arg);
  138. break;
  139. case WDIOC_KEEPALIVE:
  140. wdt_service();
  141. ret = 0;
  142. break;
  143. case WDIOC_GETTIMEOUT:
  144. ret = put_user(heartbeat, (int *)arg);
  145. break;
  146. }
  147. return ret;
  148. }
  149. static int davinci_wdt_release(struct inode *inode, struct file *file)
  150. {
  151. wdt_service();
  152. clear_bit(WDT_IN_USE, &wdt_status);
  153. return 0;
  154. }
  155. static const struct file_operations davinci_wdt_fops = {
  156. .owner = THIS_MODULE,
  157. .llseek = no_llseek,
  158. .write = davinci_wdt_write,
  159. .unlocked_ioctl = davinci_wdt_ioctl,
  160. .open = davinci_wdt_open,
  161. .release = davinci_wdt_release,
  162. };
  163. static struct miscdevice davinci_wdt_miscdev = {
  164. .minor = WATCHDOG_MINOR,
  165. .name = "watchdog",
  166. .fops = &davinci_wdt_fops,
  167. };
  168. static int __devinit davinci_wdt_probe(struct platform_device *pdev)
  169. {
  170. int ret = 0, size;
  171. struct device *dev = &pdev->dev;
  172. wdt_clk = clk_get(dev, NULL);
  173. if (WARN_ON(IS_ERR(wdt_clk)))
  174. return PTR_ERR(wdt_clk);
  175. clk_enable(wdt_clk);
  176. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  177. heartbeat = DEFAULT_HEARTBEAT;
  178. dev_info(dev, "heartbeat %d sec\n", heartbeat);
  179. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  180. if (wdt_mem == NULL) {
  181. dev_err(dev, "failed to get memory region resource\n");
  182. return -ENOENT;
  183. }
  184. size = resource_size(wdt_mem);
  185. if (!request_mem_region(wdt_mem->start, size, pdev->name)) {
  186. dev_err(dev, "failed to get memory region\n");
  187. return -ENOENT;
  188. }
  189. wdt_base = ioremap(wdt_mem->start, size);
  190. if (!wdt_base) {
  191. dev_err(dev, "failed to map memory region\n");
  192. release_mem_region(wdt_mem->start, size);
  193. wdt_mem = NULL;
  194. return -ENOMEM;
  195. }
  196. ret = misc_register(&davinci_wdt_miscdev);
  197. if (ret < 0) {
  198. dev_err(dev, "cannot register misc device\n");
  199. release_mem_region(wdt_mem->start, size);
  200. wdt_mem = NULL;
  201. } else {
  202. set_bit(WDT_DEVICE_INITED, &wdt_status);
  203. }
  204. iounmap(wdt_base);
  205. return ret;
  206. }
  207. static int __devexit davinci_wdt_remove(struct platform_device *pdev)
  208. {
  209. misc_deregister(&davinci_wdt_miscdev);
  210. if (wdt_mem) {
  211. release_mem_region(wdt_mem->start, resource_size(wdt_mem));
  212. wdt_mem = NULL;
  213. }
  214. clk_disable(wdt_clk);
  215. clk_put(wdt_clk);
  216. return 0;
  217. }
  218. static struct platform_driver platform_wdt_driver = {
  219. .driver = {
  220. .name = "watchdog",
  221. .owner = THIS_MODULE,
  222. },
  223. .probe = davinci_wdt_probe,
  224. .remove = __devexit_p(davinci_wdt_remove),
  225. };
  226. static int __init davinci_wdt_init(void)
  227. {
  228. return platform_driver_register(&platform_wdt_driver);
  229. }
  230. static void __exit davinci_wdt_exit(void)
  231. {
  232. platform_driver_unregister(&platform_wdt_driver);
  233. }
  234. module_init(davinci_wdt_init);
  235. module_exit(davinci_wdt_exit);
  236. MODULE_AUTHOR("Texas Instruments");
  237. MODULE_DESCRIPTION("DaVinci Watchdog Driver");
  238. module_param(heartbeat, int, 0);
  239. MODULE_PARM_DESC(heartbeat,
  240. "Watchdog heartbeat period in seconds from 1 to "
  241. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  242. __MODULE_STRING(DEFAULT_HEARTBEAT));
  243. MODULE_LICENSE("GPL");
  244. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  245. MODULE_ALIAS("platform:watchdog");