mv64x60_wdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
  3. *
  4. * Author: James Chapman <jchapman@katalix.com>
  5. *
  6. * Platform-specific setup code should configure the dog to generate
  7. * interrupt or reset as required. This code only enables/disables
  8. * and services the watchdog.
  9. *
  10. * Derived from mpc8xx_wdt.c, with the following copyright.
  11. *
  12. * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under
  13. * the terms of the GNU General Public License version 2. This program
  14. * is licensed "as is" without any warranty of any kind, whether express
  15. * or implied.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/module.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/mv643xx.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/io.h>
  28. #define MV64x60_WDT_WDC_OFFSET 0
  29. /*
  30. * The watchdog configuration register contains a pair of 2-bit fields,
  31. * 1. a reload field, bits 27-26, which triggers a reload of
  32. * the countdown register, and
  33. * 2. an enable field, bits 25-24, which toggles between
  34. * enabling and disabling the watchdog timer.
  35. * Bit 31 is a read-only field which indicates whether the
  36. * watchdog timer is currently enabled.
  37. *
  38. * The low 24 bits contain the timer reload value.
  39. */
  40. #define MV64x60_WDC_ENABLE_SHIFT 24
  41. #define MV64x60_WDC_SERVICE_SHIFT 26
  42. #define MV64x60_WDC_ENABLED_SHIFT 31
  43. #define MV64x60_WDC_ENABLED_TRUE 1
  44. #define MV64x60_WDC_ENABLED_FALSE 0
  45. /* Flags bits */
  46. #define MV64x60_WDOG_FLAG_OPENED 0
  47. static unsigned long wdt_flags;
  48. static int wdt_status;
  49. static void __iomem *mv64x60_wdt_regs;
  50. static int mv64x60_wdt_timeout;
  51. static int mv64x60_wdt_count;
  52. static unsigned int bus_clk;
  53. static char expect_close;
  54. static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
  55. static bool nowayout = WATCHDOG_NOWAYOUT;
  56. module_param(nowayout, bool, 0);
  57. MODULE_PARM_DESC(nowayout,
  58. "Watchdog cannot be stopped once started (default="
  59. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  60. static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
  61. {
  62. u32 data;
  63. u32 enabled;
  64. int ret = 0;
  65. spin_lock(&mv64x60_wdt_spinlock);
  66. data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  67. enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
  68. /* only toggle the requested field if enabled state matches predicate */
  69. if ((enabled ^ enabled_predicate) == 0) {
  70. /* We write a 1, then a 2 -- to the appropriate field */
  71. data = (1 << field_shift) | mv64x60_wdt_count;
  72. writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  73. data = (2 << field_shift) | mv64x60_wdt_count;
  74. writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
  75. ret = 1;
  76. }
  77. spin_unlock(&mv64x60_wdt_spinlock);
  78. return ret;
  79. }
  80. static void mv64x60_wdt_service(void)
  81. {
  82. mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  83. MV64x60_WDC_SERVICE_SHIFT);
  84. }
  85. static void mv64x60_wdt_handler_enable(void)
  86. {
  87. if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
  88. MV64x60_WDC_ENABLE_SHIFT)) {
  89. mv64x60_wdt_service();
  90. pr_notice("watchdog activated\n");
  91. }
  92. }
  93. static void mv64x60_wdt_handler_disable(void)
  94. {
  95. if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
  96. MV64x60_WDC_ENABLE_SHIFT))
  97. pr_notice("watchdog deactivated\n");
  98. }
  99. static void mv64x60_wdt_set_timeout(unsigned int timeout)
  100. {
  101. /* maximum bus cycle count is 0xFFFFFFFF */
  102. if (timeout > 0xFFFFFFFF / bus_clk)
  103. timeout = 0xFFFFFFFF / bus_clk;
  104. mv64x60_wdt_count = timeout * bus_clk >> 8;
  105. mv64x60_wdt_timeout = timeout;
  106. }
  107. static int mv64x60_wdt_open(struct inode *inode, struct file *file)
  108. {
  109. if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
  110. return -EBUSY;
  111. if (nowayout)
  112. __module_get(THIS_MODULE);
  113. mv64x60_wdt_handler_enable();
  114. return nonseekable_open(inode, file);
  115. }
  116. static int mv64x60_wdt_release(struct inode *inode, struct file *file)
  117. {
  118. if (expect_close == 42)
  119. mv64x60_wdt_handler_disable();
  120. else {
  121. pr_crit("unexpected close, not stopping timer!\n");
  122. mv64x60_wdt_service();
  123. }
  124. expect_close = 0;
  125. clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
  126. return 0;
  127. }
  128. static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
  129. size_t len, loff_t *ppos)
  130. {
  131. if (len) {
  132. if (!nowayout) {
  133. size_t i;
  134. expect_close = 0;
  135. for (i = 0; i != len; i++) {
  136. char c;
  137. if (get_user(c, data + i))
  138. return -EFAULT;
  139. if (c == 'V')
  140. expect_close = 42;
  141. }
  142. }
  143. mv64x60_wdt_service();
  144. }
  145. return len;
  146. }
  147. static long mv64x60_wdt_ioctl(struct file *file,
  148. unsigned int cmd, unsigned long arg)
  149. {
  150. int timeout;
  151. int options;
  152. void __user *argp = (void __user *)arg;
  153. static const struct watchdog_info info = {
  154. .options = WDIOF_SETTIMEOUT |
  155. WDIOF_MAGICCLOSE |
  156. WDIOF_KEEPALIVEPING,
  157. .firmware_version = 0,
  158. .identity = "MV64x60 watchdog",
  159. };
  160. switch (cmd) {
  161. case WDIOC_GETSUPPORT:
  162. if (copy_to_user(argp, &info, sizeof(info)))
  163. return -EFAULT;
  164. break;
  165. case WDIOC_GETSTATUS:
  166. case WDIOC_GETBOOTSTATUS:
  167. if (put_user(wdt_status, (int __user *)argp))
  168. return -EFAULT;
  169. wdt_status &= ~WDIOF_KEEPALIVEPING;
  170. break;
  171. case WDIOC_GETTEMP:
  172. return -EOPNOTSUPP;
  173. case WDIOC_SETOPTIONS:
  174. if (get_user(options, (int __user *)argp))
  175. return -EFAULT;
  176. if (options & WDIOS_DISABLECARD)
  177. mv64x60_wdt_handler_disable();
  178. if (options & WDIOS_ENABLECARD)
  179. mv64x60_wdt_handler_enable();
  180. break;
  181. case WDIOC_KEEPALIVE:
  182. mv64x60_wdt_service();
  183. wdt_status |= WDIOF_KEEPALIVEPING;
  184. break;
  185. case WDIOC_SETTIMEOUT:
  186. if (get_user(timeout, (int __user *)argp))
  187. return -EFAULT;
  188. mv64x60_wdt_set_timeout(timeout);
  189. /* Fall through */
  190. case WDIOC_GETTIMEOUT:
  191. if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
  192. return -EFAULT;
  193. break;
  194. default:
  195. return -ENOTTY;
  196. }
  197. return 0;
  198. }
  199. static const struct file_operations mv64x60_wdt_fops = {
  200. .owner = THIS_MODULE,
  201. .llseek = no_llseek,
  202. .write = mv64x60_wdt_write,
  203. .unlocked_ioctl = mv64x60_wdt_ioctl,
  204. .open = mv64x60_wdt_open,
  205. .release = mv64x60_wdt_release,
  206. };
  207. static struct miscdevice mv64x60_wdt_miscdev = {
  208. .minor = WATCHDOG_MINOR,
  209. .name = "watchdog",
  210. .fops = &mv64x60_wdt_fops,
  211. };
  212. static int mv64x60_wdt_probe(struct platform_device *dev)
  213. {
  214. struct mv64x60_wdt_pdata *pdata = dev_get_platdata(&dev->dev);
  215. struct resource *r;
  216. int timeout = 10;
  217. bus_clk = 133; /* in MHz */
  218. if (pdata) {
  219. timeout = pdata->timeout;
  220. bus_clk = pdata->bus_clk;
  221. }
  222. /* Since bus_clk is truncated MHz, actual frequency could be
  223. * up to 1MHz higher. Round up, since it's better to time out
  224. * too late than too soon.
  225. */
  226. bus_clk++;
  227. bus_clk *= 1000000; /* convert to Hz */
  228. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  229. if (!r)
  230. return -ENODEV;
  231. mv64x60_wdt_regs = devm_ioremap(&dev->dev, r->start, resource_size(r));
  232. if (mv64x60_wdt_regs == NULL)
  233. return -ENOMEM;
  234. mv64x60_wdt_set_timeout(timeout);
  235. mv64x60_wdt_handler_disable(); /* in case timer was already running */
  236. return misc_register(&mv64x60_wdt_miscdev);
  237. }
  238. static int mv64x60_wdt_remove(struct platform_device *dev)
  239. {
  240. misc_deregister(&mv64x60_wdt_miscdev);
  241. mv64x60_wdt_handler_disable();
  242. return 0;
  243. }
  244. static struct platform_driver mv64x60_wdt_driver = {
  245. .probe = mv64x60_wdt_probe,
  246. .remove = mv64x60_wdt_remove,
  247. .driver = {
  248. .name = MV64x60_WDT_NAME,
  249. },
  250. };
  251. static int __init mv64x60_wdt_init(void)
  252. {
  253. pr_info("MV64x60 watchdog driver\n");
  254. return platform_driver_register(&mv64x60_wdt_driver);
  255. }
  256. static void __exit mv64x60_wdt_exit(void)
  257. {
  258. platform_driver_unregister(&mv64x60_wdt_driver);
  259. }
  260. module_init(mv64x60_wdt_init);
  261. module_exit(mv64x60_wdt_exit);
  262. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  263. MODULE_DESCRIPTION("MV64x60 watchdog driver");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:" MV64x60_WDT_NAME);