m54xx_wdt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * drivers/watchdog/m54xx_wdt.c
  3. *
  4. * Watchdog driver for ColdFire MCF547x & MCF548x processors
  5. * Copyright 2010 (c) Philippe De Muyter <phdm@macqel.be>
  6. *
  7. * Adapted from the IXP4xx watchdog driver, which carries these notices:
  8. *
  9. * Author: Deepak Saxena <dsaxena@plexity.net>
  10. *
  11. * Copyright 2004 (c) MontaVista, Software, Inc.
  12. * Based on sa1100 driver, Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/fs.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/init.h>
  26. #include <linux/bitops.h>
  27. #include <linux/ioport.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/coldfire.h>
  30. #include <asm/m54xxsim.h>
  31. #include <asm/m54xxgpt.h>
  32. static int nowayout = WATCHDOG_NOWAYOUT;
  33. static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
  34. static unsigned long wdt_status;
  35. #define WDT_IN_USE 0
  36. #define WDT_OK_TO_CLOSE 1
  37. static void wdt_enable(void)
  38. {
  39. unsigned int gms0;
  40. /* preserve GPIO usage, if any */
  41. gms0 = __raw_readl(MCF_MBAR + MCF_GPT_GMS0);
  42. if (gms0 & MCF_GPT_GMS_TMS_GPIO)
  43. gms0 &= (MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_GPIO_MASK
  44. | MCF_GPT_GMS_OD);
  45. else
  46. gms0 = MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_OD;
  47. __raw_writel(gms0, MCF_MBAR + MCF_GPT_GMS0);
  48. __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
  49. MCF_GPT_GCIR_CNT(0xffff), MCF_MBAR + MCF_GPT_GCIR0);
  50. gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
  51. __raw_writel(gms0, MCF_MBAR + MCF_GPT_GMS0);
  52. }
  53. static void wdt_disable(void)
  54. {
  55. unsigned int gms0;
  56. /* disable watchdog */
  57. gms0 = __raw_readl(MCF_MBAR + MCF_GPT_GMS0);
  58. gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
  59. __raw_writel(gms0, MCF_MBAR + MCF_GPT_GMS0);
  60. }
  61. static void wdt_keepalive(void)
  62. {
  63. unsigned int gms0;
  64. gms0 = __raw_readl(MCF_MBAR + MCF_GPT_GMS0);
  65. gms0 |= MCF_GPT_GMS_OCPW(0xA5);
  66. __raw_writel(gms0, MCF_MBAR + MCF_GPT_GMS0);
  67. }
  68. static int m54xx_wdt_open(struct inode *inode, struct file *file)
  69. {
  70. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  71. return -EBUSY;
  72. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  73. wdt_enable();
  74. return nonseekable_open(inode, file);
  75. }
  76. static ssize_t m54xx_wdt_write(struct file *file, const char *data,
  77. size_t len, loff_t *ppos)
  78. {
  79. if (len) {
  80. if (!nowayout) {
  81. size_t i;
  82. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  83. for (i = 0; i != len; i++) {
  84. char c;
  85. if (get_user(c, data + i))
  86. return -EFAULT;
  87. if (c == 'V')
  88. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  89. }
  90. }
  91. wdt_keepalive();
  92. }
  93. return len;
  94. }
  95. static const struct watchdog_info ident = {
  96. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  97. WDIOF_KEEPALIVEPING,
  98. .identity = "Coldfire M54xx Watchdog",
  99. };
  100. static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
  101. unsigned long arg)
  102. {
  103. int ret = -ENOTTY;
  104. int time;
  105. switch (cmd) {
  106. case WDIOC_GETSUPPORT:
  107. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  108. sizeof(ident)) ? -EFAULT : 0;
  109. break;
  110. case WDIOC_GETSTATUS:
  111. ret = put_user(0, (int *)arg);
  112. break;
  113. case WDIOC_GETBOOTSTATUS:
  114. ret = put_user(0, (int *)arg);
  115. break;
  116. case WDIOC_KEEPALIVE:
  117. wdt_keepalive();
  118. ret = 0;
  119. break;
  120. case WDIOC_SETTIMEOUT:
  121. ret = get_user(time, (int *)arg);
  122. if (ret)
  123. break;
  124. if (time <= 0 || time > 30) {
  125. ret = -EINVAL;
  126. break;
  127. }
  128. heartbeat = time;
  129. wdt_enable();
  130. /* Fall through */
  131. case WDIOC_GETTIMEOUT:
  132. ret = put_user(heartbeat, (int *)arg);
  133. break;
  134. }
  135. return ret;
  136. }
  137. static int m54xx_wdt_release(struct inode *inode, struct file *file)
  138. {
  139. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  140. wdt_disable();
  141. else {
  142. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  143. "timer will not stop\n");
  144. wdt_keepalive();
  145. }
  146. clear_bit(WDT_IN_USE, &wdt_status);
  147. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  148. return 0;
  149. }
  150. static const struct file_operations m54xx_wdt_fops = {
  151. .owner = THIS_MODULE,
  152. .llseek = no_llseek,
  153. .write = m54xx_wdt_write,
  154. .unlocked_ioctl = m54xx_wdt_ioctl,
  155. .open = m54xx_wdt_open,
  156. .release = m54xx_wdt_release,
  157. };
  158. static struct miscdevice m54xx_wdt_miscdev = {
  159. .minor = WATCHDOG_MINOR,
  160. .name = "watchdog",
  161. .fops = &m54xx_wdt_fops,
  162. };
  163. static int __init m54xx_wdt_init(void)
  164. {
  165. if (!request_mem_region(MCF_MBAR + MCF_GPT_GCIR0, 4,
  166. "Coldfire M54xx Watchdog")) {
  167. printk(KERN_WARNING
  168. "Coldfire M54xx Watchdog : I/O region busy\n");
  169. return -EBUSY;
  170. }
  171. printk(KERN_INFO "ColdFire watchdog driver is loaded.\n");
  172. return misc_register(&m54xx_wdt_miscdev);
  173. }
  174. static void __exit m54xx_wdt_exit(void)
  175. {
  176. misc_deregister(&m54xx_wdt_miscdev);
  177. release_mem_region(MCF_MBAR + MCF_GPT_GCIR0, 4);
  178. }
  179. module_init(m54xx_wdt_init);
  180. module_exit(m54xx_wdt_exit);
  181. MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
  182. MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
  183. module_param(heartbeat, int, 0);
  184. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
  185. module_param(nowayout, int, 0);
  186. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  187. MODULE_LICENSE("GPL");
  188. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);