lantiq_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  7. * Based on EP93xx wdt driver
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/watchdog.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <lantiq.h>
  19. /* Section 3.4 of the datasheet
  20. * The password sequence protects the WDT control register from unintended
  21. * write actions, which might cause malfunction of the WDT.
  22. *
  23. * essentially the following two magic passwords need to be written to allow
  24. * IO access to the WDT core
  25. */
  26. #define LTQ_WDT_PW1 0x00BE0000
  27. #define LTQ_WDT_PW2 0x00DC0000
  28. #define LTQ_WDT_CR 0x0 /* watchdog control register */
  29. #define LTQ_WDT_SR 0x8 /* watchdog status register */
  30. #define LTQ_WDT_SR_EN (0x1 << 31) /* enable bit */
  31. #define LTQ_WDT_SR_PWD (0x3 << 26) /* turn on power */
  32. #define LTQ_WDT_SR_CLKDIV (0x3 << 24) /* turn on clock and set */
  33. /* divider to 0x40000 */
  34. #define LTQ_WDT_DIVIDER 0x40000
  35. #define LTQ_MAX_TIMEOUT ((1 << 16) - 1) /* the reload field is 16 bit */
  36. static bool nowayout = WATCHDOG_NOWAYOUT;
  37. static void __iomem *ltq_wdt_membase;
  38. static unsigned long ltq_io_region_clk_rate;
  39. static unsigned long ltq_wdt_bootstatus;
  40. static unsigned long ltq_wdt_in_use;
  41. static int ltq_wdt_timeout = 30;
  42. static int ltq_wdt_ok_to_close;
  43. static void
  44. ltq_wdt_enable(void)
  45. {
  46. unsigned long int timeout = ltq_wdt_timeout *
  47. (ltq_io_region_clk_rate / LTQ_WDT_DIVIDER) + 0x1000;
  48. if (timeout > LTQ_MAX_TIMEOUT)
  49. timeout = LTQ_MAX_TIMEOUT;
  50. /* write the first password magic */
  51. ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
  52. /* write the second magic plus the configuration and new timeout */
  53. ltq_w32(LTQ_WDT_SR_EN | LTQ_WDT_SR_PWD | LTQ_WDT_SR_CLKDIV |
  54. LTQ_WDT_PW2 | timeout, ltq_wdt_membase + LTQ_WDT_CR);
  55. }
  56. static void
  57. ltq_wdt_disable(void)
  58. {
  59. /* write the first password magic */
  60. ltq_w32(LTQ_WDT_PW1, ltq_wdt_membase + LTQ_WDT_CR);
  61. /* write the second password magic with no config
  62. * this turns the watchdog off
  63. */
  64. ltq_w32(LTQ_WDT_PW2, ltq_wdt_membase + LTQ_WDT_CR);
  65. }
  66. static ssize_t
  67. ltq_wdt_write(struct file *file, const char __user *data,
  68. size_t len, loff_t *ppos)
  69. {
  70. if (len) {
  71. if (!nowayout) {
  72. size_t i;
  73. ltq_wdt_ok_to_close = 0;
  74. for (i = 0; i != len; i++) {
  75. char c;
  76. if (get_user(c, data + i))
  77. return -EFAULT;
  78. if (c == 'V')
  79. ltq_wdt_ok_to_close = 1;
  80. else
  81. ltq_wdt_ok_to_close = 0;
  82. }
  83. }
  84. ltq_wdt_enable();
  85. }
  86. return len;
  87. }
  88. static struct watchdog_info ident = {
  89. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  90. WDIOF_CARDRESET,
  91. .identity = "ltq_wdt",
  92. };
  93. static long
  94. ltq_wdt_ioctl(struct file *file,
  95. unsigned int cmd, unsigned long arg)
  96. {
  97. int ret = -ENOTTY;
  98. switch (cmd) {
  99. case WDIOC_GETSUPPORT:
  100. ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
  101. sizeof(ident)) ? -EFAULT : 0;
  102. break;
  103. case WDIOC_GETBOOTSTATUS:
  104. ret = put_user(ltq_wdt_bootstatus, (int __user *)arg);
  105. break;
  106. case WDIOC_GETSTATUS:
  107. ret = put_user(0, (int __user *)arg);
  108. break;
  109. case WDIOC_SETTIMEOUT:
  110. ret = get_user(ltq_wdt_timeout, (int __user *)arg);
  111. if (!ret)
  112. ltq_wdt_enable();
  113. /* intentional drop through */
  114. case WDIOC_GETTIMEOUT:
  115. ret = put_user(ltq_wdt_timeout, (int __user *)arg);
  116. break;
  117. case WDIOC_KEEPALIVE:
  118. ltq_wdt_enable();
  119. ret = 0;
  120. break;
  121. }
  122. return ret;
  123. }
  124. static int
  125. ltq_wdt_open(struct inode *inode, struct file *file)
  126. {
  127. if (test_and_set_bit(0, &ltq_wdt_in_use))
  128. return -EBUSY;
  129. ltq_wdt_in_use = 1;
  130. ltq_wdt_enable();
  131. return nonseekable_open(inode, file);
  132. }
  133. static int
  134. ltq_wdt_release(struct inode *inode, struct file *file)
  135. {
  136. if (ltq_wdt_ok_to_close)
  137. ltq_wdt_disable();
  138. else
  139. pr_err("watchdog closed without warning\n");
  140. ltq_wdt_ok_to_close = 0;
  141. clear_bit(0, &ltq_wdt_in_use);
  142. return 0;
  143. }
  144. static const struct file_operations ltq_wdt_fops = {
  145. .owner = THIS_MODULE,
  146. .write = ltq_wdt_write,
  147. .unlocked_ioctl = ltq_wdt_ioctl,
  148. .open = ltq_wdt_open,
  149. .release = ltq_wdt_release,
  150. .llseek = no_llseek,
  151. };
  152. static struct miscdevice ltq_wdt_miscdev = {
  153. .minor = WATCHDOG_MINOR,
  154. .name = "watchdog",
  155. .fops = &ltq_wdt_fops,
  156. };
  157. static int __init
  158. ltq_wdt_probe(struct platform_device *pdev)
  159. {
  160. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. struct clk *clk;
  162. if (!res) {
  163. dev_err(&pdev->dev, "cannot obtain I/O memory region");
  164. return -ENOENT;
  165. }
  166. res = devm_request_mem_region(&pdev->dev, res->start,
  167. resource_size(res), dev_name(&pdev->dev));
  168. if (!res) {
  169. dev_err(&pdev->dev, "cannot request I/O memory region");
  170. return -EBUSY;
  171. }
  172. ltq_wdt_membase = devm_ioremap_nocache(&pdev->dev, res->start,
  173. resource_size(res));
  174. if (!ltq_wdt_membase) {
  175. dev_err(&pdev->dev, "cannot remap I/O memory region\n");
  176. return -ENOMEM;
  177. }
  178. /* we do not need to enable the clock as it is always running */
  179. clk = clk_get(&pdev->dev, "io");
  180. WARN_ON(!clk);
  181. ltq_io_region_clk_rate = clk_get_rate(clk);
  182. clk_put(clk);
  183. if (ltq_reset_cause() == LTQ_RST_CAUSE_WDTRST)
  184. ltq_wdt_bootstatus = WDIOF_CARDRESET;
  185. return misc_register(&ltq_wdt_miscdev);
  186. }
  187. static int __devexit
  188. ltq_wdt_remove(struct platform_device *pdev)
  189. {
  190. misc_deregister(&ltq_wdt_miscdev);
  191. return 0;
  192. }
  193. static struct platform_driver ltq_wdt_driver = {
  194. .remove = __devexit_p(ltq_wdt_remove),
  195. .driver = {
  196. .name = "ltq_wdt",
  197. .owner = THIS_MODULE,
  198. },
  199. };
  200. static int __init
  201. init_ltq_wdt(void)
  202. {
  203. return platform_driver_probe(&ltq_wdt_driver, ltq_wdt_probe);
  204. }
  205. static void __exit
  206. exit_ltq_wdt(void)
  207. {
  208. return platform_driver_unregister(&ltq_wdt_driver);
  209. }
  210. module_init(init_ltq_wdt);
  211. module_exit(exit_ltq_wdt);
  212. module_param(nowayout, bool, 0);
  213. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  214. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  215. MODULE_DESCRIPTION("Lantiq SoC Watchdog");
  216. MODULE_LICENSE("GPL");
  217. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);