m54xx_wdt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <linux/io.h>
  31. #include <asm/coldfire.h>
  32. #include <asm/m54xxsim.h>
  33. #include <asm/m54xxgpt.h>
  34. static bool nowayout = WATCHDOG_NOWAYOUT;
  35. static unsigned int heartbeat = 30; /* (secs) Default is 0.5 minute */
  36. static unsigned long wdt_status;
  37. #define WDT_IN_USE 0
  38. #define WDT_OK_TO_CLOSE 1
  39. static void wdt_enable(void)
  40. {
  41. unsigned int gms0;
  42. /* preserve GPIO usage, if any */
  43. gms0 = __raw_readl(MCF_GPT_GMS0);
  44. if (gms0 & MCF_GPT_GMS_TMS_GPIO)
  45. gms0 &= (MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_GPIO_MASK
  46. | MCF_GPT_GMS_OD);
  47. else
  48. gms0 = MCF_GPT_GMS_TMS_GPIO | MCF_GPT_GMS_OD;
  49. __raw_writel(gms0, MCF_GPT_GMS0);
  50. __raw_writel(MCF_GPT_GCIR_PRE(heartbeat*(MCF_BUSCLK/0xffff)) |
  51. MCF_GPT_GCIR_CNT(0xffff), MCF_GPT_GCIR0);
  52. gms0 |= MCF_GPT_GMS_OCPW(0xA5) | MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE;
  53. __raw_writel(gms0, MCF_GPT_GMS0);
  54. }
  55. static void wdt_disable(void)
  56. {
  57. unsigned int gms0;
  58. /* disable watchdog */
  59. gms0 = __raw_readl(MCF_GPT_GMS0);
  60. gms0 &= ~(MCF_GPT_GMS_WDEN | MCF_GPT_GMS_CE);
  61. __raw_writel(gms0, MCF_GPT_GMS0);
  62. }
  63. static void wdt_keepalive(void)
  64. {
  65. unsigned int gms0;
  66. gms0 = __raw_readl(MCF_GPT_GMS0);
  67. gms0 |= MCF_GPT_GMS_OCPW(0xA5);
  68. __raw_writel(gms0, MCF_GPT_GMS0);
  69. }
  70. static int m54xx_wdt_open(struct inode *inode, struct file *file)
  71. {
  72. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  73. return -EBUSY;
  74. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  75. wdt_enable();
  76. return stream_open(inode, file);
  77. }
  78. static ssize_t m54xx_wdt_write(struct file *file, const char *data,
  79. size_t len, loff_t *ppos)
  80. {
  81. if (len) {
  82. if (!nowayout) {
  83. size_t i;
  84. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  85. for (i = 0; i != len; i++) {
  86. char c;
  87. if (get_user(c, data + i))
  88. return -EFAULT;
  89. if (c == 'V')
  90. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  91. }
  92. }
  93. wdt_keepalive();
  94. }
  95. return len;
  96. }
  97. static const struct watchdog_info ident = {
  98. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  99. WDIOF_KEEPALIVEPING,
  100. .identity = "Coldfire M54xx Watchdog",
  101. };
  102. static long m54xx_wdt_ioctl(struct file *file, unsigned int cmd,
  103. unsigned long arg)
  104. {
  105. int ret = -ENOTTY;
  106. int time;
  107. switch (cmd) {
  108. case WDIOC_GETSUPPORT:
  109. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  110. sizeof(ident)) ? -EFAULT : 0;
  111. break;
  112. case WDIOC_GETSTATUS:
  113. ret = put_user(0, (int *)arg);
  114. break;
  115. case WDIOC_GETBOOTSTATUS:
  116. ret = put_user(0, (int *)arg);
  117. break;
  118. case WDIOC_KEEPALIVE:
  119. wdt_keepalive();
  120. ret = 0;
  121. break;
  122. case WDIOC_SETTIMEOUT:
  123. ret = get_user(time, (int *)arg);
  124. if (ret)
  125. break;
  126. if (time <= 0 || time > 30) {
  127. ret = -EINVAL;
  128. break;
  129. }
  130. heartbeat = time;
  131. wdt_enable();
  132. fallthrough;
  133. case WDIOC_GETTIMEOUT:
  134. ret = put_user(heartbeat, (int *)arg);
  135. break;
  136. }
  137. return ret;
  138. }
  139. static int m54xx_wdt_release(struct inode *inode, struct file *file)
  140. {
  141. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  142. wdt_disable();
  143. else {
  144. pr_crit("Device closed unexpectedly - timer will not stop\n");
  145. wdt_keepalive();
  146. }
  147. clear_bit(WDT_IN_USE, &wdt_status);
  148. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  149. return 0;
  150. }
  151. static const struct file_operations m54xx_wdt_fops = {
  152. .owner = THIS_MODULE,
  153. .llseek = no_llseek,
  154. .write = m54xx_wdt_write,
  155. .unlocked_ioctl = m54xx_wdt_ioctl,
  156. .compat_ioctl = compat_ptr_ioctl,
  157. .open = m54xx_wdt_open,
  158. .release = m54xx_wdt_release,
  159. };
  160. static struct miscdevice m54xx_wdt_miscdev = {
  161. .minor = WATCHDOG_MINOR,
  162. .name = "watchdog",
  163. .fops = &m54xx_wdt_fops,
  164. };
  165. static int __init m54xx_wdt_init(void)
  166. {
  167. if (!request_mem_region(MCF_GPT_GCIR0, 4, "Coldfire M54xx Watchdog")) {
  168. pr_warn("I/O region busy\n");
  169. return -EBUSY;
  170. }
  171. pr_info("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_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, bool, 0);
  186. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  187. MODULE_LICENSE("GPL");