m54xx_wdt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/fs.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/init.h>
  27. #include <linux/bitops.h>
  28. #include <linux/ioport.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/coldfire.h>
  31. #include <asm/m54xxsim.h>
  32. #include <asm/m54xxgpt.h>
  33. static bool nowayout = WATCHDOG_NOWAYOUT;
  34. static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
  35. static unsigned long wdt_status;
  36. #define WDT_IN_USE 0
  37. #define WDT_OK_TO_CLOSE 1
  38. static void wdt_enable(void)
  39. {
  40. unsigned int gms0;
  41. /* preserve GPIO usage, if any */
  42. gms0 = __raw_readl(MCF_GPT_GMS0);
  43. if (gms0 & MCF_GPT_GMS_TMS_GPIO)
  44. gms0 &= (MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_GPIO_MASK
  45. | MCF_GPT_GMS_OD);
  46. else
  47. gms0 = MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_OD;
  48. __raw_writel(gms0, MCF_GPT_GMS0);
  49. __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
  50. MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0);
  51. gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
  52. __raw_writel(gms0, MCF_GPT_GMS0);
  53. }
  54. static void wdt_disable(void)
  55. {
  56. unsigned int gms0;
  57. /* disable watchdog */
  58. gms0 = __raw_readl(MCF_GPT_GMS0);
  59. gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
  60. __raw_writel(gms0, MCF_GPT_GMS0);
  61. }
  62. static void wdt_keepalive(void)
  63. {
  64. unsigned int gms0;
  65. gms0 = __raw_readl(MCF_GPT_GMS0);
  66. gms0 |= MCF_GPT_GMS_OCPW(0xA5);
  67. __raw_writel(gms0, MCF_GPT_GMS0);
  68. }
  69. static int m54xx_wdt_open(struct inode *inode, struct file *file)
  70. {
  71. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  72. return -EBUSY;
  73. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  74. wdt_enable();
  75. return nonseekable_open(inode, file);
  76. }
  77. static ssize_t m54xx_wdt_write(struct file *file, const char *data,
  78. size_t len, loff_t *ppos)
  79. {
  80. if (len) {
  81. if (!nowayout) {
  82. size_t i;
  83. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  84. for (i = 0; i != len; i++) {
  85. char c;
  86. if (get_user(c, data + i))
  87. return -EFAULT;
  88. if (c == 'V')
  89. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  90. }
  91. }
  92. wdt_keepalive();
  93. }
  94. return len;
  95. }
  96. static const struct watchdog_info ident = {
  97. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  98. WDIOF_KEEPALIVEPING,
  99. .identity = "Coldfire M54xx Watchdog",
  100. };
  101. static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
  102. unsigned long arg)
  103. {
  104. int ret = -ENOTTY;
  105. int time;
  106. switch (cmd) {
  107. case WDIOC_GETSUPPORT:
  108. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  109. sizeof(ident)) ? -EFAULT : 0;
  110. break;
  111. case WDIOC_GETSTATUS:
  112. ret = put_user(0, (int *)arg);
  113. break;
  114. case WDIOC_GETBOOTSTATUS:
  115. ret = put_user(0, (int *)arg);
  116. break;
  117. case WDIOC_KEEPALIVE:
  118. wdt_keepalive();
  119. ret = 0;
  120. break;
  121. case WDIOC_SETTIMEOUT:
  122. ret = get_user(time, (int *)arg);
  123. if (ret)
  124. break;
  125. if (time <= 0 || time > 30) {
  126. ret = -EINVAL;
  127. break;
  128. }
  129. heartbeat = time;
  130. wdt_enable();
  131. /* Fall through */
  132. case WDIOC_GETTIMEOUT:
  133. ret = put_user(heartbeat, (int *)arg);
  134. break;
  135. }
  136. return ret;
  137. }
  138. static int m54xx_wdt_release(struct inode *inode, struct file *file)
  139. {
  140. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  141. wdt_disable();
  142. else {
  143. pr_crit("Device closed unexpectedly - 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_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
  166. pr_warn("I/O region busy\n");
  167. return -EBUSY;
  168. }
  169. pr_info("driver is loaded\n");
  170. return misc_register(&m54xx_wdt_miscdev);
  171. }
  172. static void __exit m54xx_wdt_exit(void)
  173. {
  174. misc_deregister(&m54xx_wdt_miscdev);
  175. release_mem_region(MCF_GPT_GCIR0, 4);
  176. }
  177. module_init(m54xx_wdt_init);
  178. module_exit(m54xx_wdt_exit);
  179. MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
  180. MODULE_DESCRIPTION("Coldfire M54xx Watchdog");
  181. module_param(heartbeat, int, 0);
  182. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds (default 30s)");
  183. module_param(nowayout, bool, 0);
  184. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  185. MODULE_LICENSE("GPL");