jz4740_wdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
  3. * JZ4740 Watchdog driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fs.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/watchdog.h>
  22. #include <linux/init.h>
  23. #include <linux/bitops.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/io.h>
  28. #include <linux/device.h>
  29. #include <linux/clk.h>
  30. #include <linux/slab.h>
  31. #include <asm/mach-jz4740/timer.h>
  32. #define JZ_REG_WDT_TIMER_DATA 0x0
  33. #define JZ_REG_WDT_COUNTER_ENABLE 0x4
  34. #define JZ_REG_WDT_TIMER_COUNTER 0x8
  35. #define JZ_REG_WDT_TIMER_CONTROL 0xC
  36. #define JZ_WDT_CLOCK_PCLK 0x1
  37. #define JZ_WDT_CLOCK_RTC 0x2
  38. #define JZ_WDT_CLOCK_EXT 0x4
  39. #define WDT_IN_USE 0
  40. #define WDT_OK_TO_CLOSE 1
  41. #define JZ_WDT_CLOCK_DIV_SHIFT 3
  42. #define JZ_WDT_CLOCK_DIV_1 (0 << JZ_WDT_CLOCK_DIV_SHIFT)
  43. #define JZ_WDT_CLOCK_DIV_4 (1 << JZ_WDT_CLOCK_DIV_SHIFT)
  44. #define JZ_WDT_CLOCK_DIV_16 (2 << JZ_WDT_CLOCK_DIV_SHIFT)
  45. #define JZ_WDT_CLOCK_DIV_64 (3 << JZ_WDT_CLOCK_DIV_SHIFT)
  46. #define JZ_WDT_CLOCK_DIV_256 (4 << JZ_WDT_CLOCK_DIV_SHIFT)
  47. #define JZ_WDT_CLOCK_DIV_1024 (5 << JZ_WDT_CLOCK_DIV_SHIFT)
  48. #define DEFAULT_HEARTBEAT 5
  49. #define MAX_HEARTBEAT 2048
  50. static struct {
  51. void __iomem *base;
  52. struct resource *mem;
  53. struct clk *rtc_clk;
  54. unsigned long status;
  55. } jz4740_wdt;
  56. static int heartbeat = DEFAULT_HEARTBEAT;
  57. static void jz4740_wdt_service(void)
  58. {
  59. writew(0x0, jz4740_wdt.base + JZ_REG_WDT_TIMER_COUNTER);
  60. }
  61. static void jz4740_wdt_set_heartbeat(int new_heartbeat)
  62. {
  63. unsigned int rtc_clk_rate;
  64. unsigned int timeout_value;
  65. unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
  66. heartbeat = new_heartbeat;
  67. rtc_clk_rate = clk_get_rate(jz4740_wdt.rtc_clk);
  68. timeout_value = rtc_clk_rate * heartbeat;
  69. while (timeout_value > 0xffff) {
  70. if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
  71. /* Requested timeout too high;
  72. * use highest possible value. */
  73. timeout_value = 0xffff;
  74. break;
  75. }
  76. timeout_value >>= 2;
  77. clock_div += (1 << JZ_WDT_CLOCK_DIV_SHIFT);
  78. }
  79. writeb(0x0, jz4740_wdt.base + JZ_REG_WDT_COUNTER_ENABLE);
  80. writew(clock_div, jz4740_wdt.base + JZ_REG_WDT_TIMER_CONTROL);
  81. writew((u16)timeout_value, jz4740_wdt.base + JZ_REG_WDT_TIMER_DATA);
  82. writew(0x0, jz4740_wdt.base + JZ_REG_WDT_TIMER_COUNTER);
  83. writew(clock_div | JZ_WDT_CLOCK_RTC,
  84. jz4740_wdt.base + JZ_REG_WDT_TIMER_CONTROL);
  85. writeb(0x1, jz4740_wdt.base + JZ_REG_WDT_COUNTER_ENABLE);
  86. }
  87. static void jz4740_wdt_enable(void)
  88. {
  89. jz4740_timer_enable_watchdog();
  90. jz4740_wdt_set_heartbeat(heartbeat);
  91. }
  92. static void jz4740_wdt_disable(void)
  93. {
  94. jz4740_timer_disable_watchdog();
  95. writeb(0x0, jz4740_wdt.base + JZ_REG_WDT_COUNTER_ENABLE);
  96. }
  97. static int jz4740_wdt_open(struct inode *inode, struct file *file)
  98. {
  99. if (test_and_set_bit(WDT_IN_USE, &jz4740_wdt.status))
  100. return -EBUSY;
  101. jz4740_wdt_enable();
  102. return nonseekable_open(inode, file);
  103. }
  104. static ssize_t jz4740_wdt_write(struct file *file, const char *data,
  105. size_t len, loff_t *ppos)
  106. {
  107. if (len) {
  108. size_t i;
  109. clear_bit(WDT_OK_TO_CLOSE, &jz4740_wdt.status);
  110. for (i = 0; i != len; i++) {
  111. char c;
  112. if (get_user(c, data + i))
  113. return -EFAULT;
  114. if (c == 'V')
  115. set_bit(WDT_OK_TO_CLOSE, &jz4740_wdt.status);
  116. }
  117. jz4740_wdt_service();
  118. }
  119. return len;
  120. }
  121. static const struct watchdog_info ident = {
  122. .options = WDIOF_KEEPALIVEPING,
  123. .identity = "jz4740 Watchdog",
  124. };
  125. static long jz4740_wdt_ioctl(struct file *file,
  126. unsigned int cmd, unsigned long arg)
  127. {
  128. int ret = -ENOTTY;
  129. int heartbeat_seconds;
  130. switch (cmd) {
  131. case WDIOC_GETSUPPORT:
  132. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  133. sizeof(ident)) ? -EFAULT : 0;
  134. break;
  135. case WDIOC_GETSTATUS:
  136. case WDIOC_GETBOOTSTATUS:
  137. ret = put_user(0, (int *)arg);
  138. break;
  139. case WDIOC_KEEPALIVE:
  140. jz4740_wdt_service();
  141. return 0;
  142. case WDIOC_SETTIMEOUT:
  143. if (get_user(heartbeat_seconds, (int __user *)arg))
  144. return -EFAULT;
  145. jz4740_wdt_set_heartbeat(heartbeat_seconds);
  146. return 0;
  147. case WDIOC_GETTIMEOUT:
  148. return put_user(heartbeat, (int *)arg);
  149. default:
  150. break;
  151. }
  152. return ret;
  153. }
  154. static int jz4740_wdt_release(struct inode *inode, struct file *file)
  155. {
  156. jz4740_wdt_service();
  157. if (test_and_clear_bit(WDT_OK_TO_CLOSE, &jz4740_wdt.status))
  158. jz4740_wdt_disable();
  159. clear_bit(WDT_IN_USE, &jz4740_wdt.status);
  160. return 0;
  161. }
  162. static const struct file_operations jz4740_wdt_fops = {
  163. .owner = THIS_MODULE,
  164. .llseek = no_llseek,
  165. .write = jz4740_wdt_write,
  166. .unlocked_ioctl = jz4740_wdt_ioctl,
  167. .open = jz4740_wdt_open,
  168. .release = jz4740_wdt_release,
  169. };
  170. static struct miscdevice jz4740_wdt_miscdev = {
  171. .minor = WATCHDOG_MINOR,
  172. .name = "watchdog",
  173. .fops = &jz4740_wdt_fops,
  174. };
  175. static int __devinit jz4740_wdt_probe(struct platform_device *pdev)
  176. {
  177. int ret = 0, size;
  178. struct resource *res;
  179. struct device *dev = &pdev->dev;
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  181. if (res == NULL) {
  182. dev_err(dev, "failed to get memory region resource\n");
  183. return -ENXIO;
  184. }
  185. size = resource_size(res);
  186. jz4740_wdt.mem = request_mem_region(res->start, size, pdev->name);
  187. if (jz4740_wdt.mem == NULL) {
  188. dev_err(dev, "failed to get memory region\n");
  189. return -EBUSY;
  190. }
  191. jz4740_wdt.base = ioremap_nocache(res->start, size);
  192. if (jz4740_wdt.base == NULL) {
  193. dev_err(dev, "failed to map memory region\n");
  194. ret = -EBUSY;
  195. goto err_release_region;
  196. }
  197. jz4740_wdt.rtc_clk = clk_get(NULL, "rtc");
  198. if (IS_ERR(jz4740_wdt.rtc_clk)) {
  199. dev_err(dev, "cannot find RTC clock\n");
  200. ret = PTR_ERR(jz4740_wdt.rtc_clk);
  201. goto err_iounmap;
  202. }
  203. ret = misc_register(&jz4740_wdt_miscdev);
  204. if (ret < 0) {
  205. dev_err(dev, "cannot register misc device\n");
  206. goto err_disable_clk;
  207. }
  208. return 0;
  209. err_disable_clk:
  210. clk_put(jz4740_wdt.rtc_clk);
  211. err_iounmap:
  212. iounmap(jz4740_wdt.base);
  213. err_release_region:
  214. release_mem_region(jz4740_wdt.mem->start,
  215. resource_size(jz4740_wdt.mem));
  216. return ret;
  217. }
  218. static int __devexit jz4740_wdt_remove(struct platform_device *pdev)
  219. {
  220. jz4740_wdt_disable();
  221. misc_deregister(&jz4740_wdt_miscdev);
  222. clk_put(jz4740_wdt.rtc_clk);
  223. iounmap(jz4740_wdt.base);
  224. jz4740_wdt.base = NULL;
  225. release_mem_region(jz4740_wdt.mem->start,
  226. resource_size(jz4740_wdt.mem));
  227. jz4740_wdt.mem = NULL;
  228. return 0;
  229. }
  230. static struct platform_driver jz4740_wdt_driver = {
  231. .probe = jz4740_wdt_probe,
  232. .remove = __devexit_p(jz4740_wdt_remove),
  233. .driver = {
  234. .name = "jz4740-wdt",
  235. .owner = THIS_MODULE,
  236. },
  237. };
  238. static int __init jz4740_wdt_init(void)
  239. {
  240. return platform_driver_register(&jz4740_wdt_driver);
  241. }
  242. module_init(jz4740_wdt_init);
  243. static void __exit jz4740_wdt_exit(void)
  244. {
  245. platform_driver_unregister(&jz4740_wdt_driver);
  246. }
  247. module_exit(jz4740_wdt_exit);
  248. MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
  249. MODULE_DESCRIPTION("jz4740 Watchdog Driver");
  250. module_param(heartbeat, int, 0);
  251. MODULE_PARM_DESC(heartbeat,
  252. "Watchdog heartbeat period in seconds from 1 to "
  253. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  254. __MODULE_STRING(DEFAULT_HEARTBEAT));
  255. MODULE_LICENSE("GPL");
  256. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  257. MODULE_ALIAS("platform:jz4740-wdt");