lantiq_wdt.c 6.1 KB

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