pnx4008_wdt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * drivers/char/watchdog/pnx4008_wdt.c
  3. *
  4. * Watchdog driver for PNX4008 board
  5. *
  6. * Authors: Dmitry Chigirev <source@mvista.com>,
  7. * Vitaly Wool <vitalywool@gmail.com>
  8. * Based on sa1100 driver,
  9. * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
  10. *
  11. * 2005-2006 (c) MontaVista Software, Inc. This file is licensed under
  12. * the terms of the GNU General Public License version 2. This program
  13. * is licensed "as is" without any warranty of any kind, whether express
  14. * or implied.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/fs.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/init.h>
  25. #include <linux/bitops.h>
  26. #include <linux/ioport.h>
  27. #include <linux/device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/clk.h>
  30. #include <asm/hardware.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/io.h>
  33. #define MODULE_NAME "PNX4008-WDT: "
  34. /* WatchDog Timer - Chapter 23 Page 207 */
  35. #define DEFAULT_HEARTBEAT 19
  36. #define MAX_HEARTBEAT 60
  37. /* Watchdog timer register set definition */
  38. #define WDTIM_INT(p) ((p) + 0x0)
  39. #define WDTIM_CTRL(p) ((p) + 0x4)
  40. #define WDTIM_COUNTER(p) ((p) + 0x8)
  41. #define WDTIM_MCTRL(p) ((p) + 0xC)
  42. #define WDTIM_MATCH0(p) ((p) + 0x10)
  43. #define WDTIM_EMR(p) ((p) + 0x14)
  44. #define WDTIM_PULSE(p) ((p) + 0x18)
  45. #define WDTIM_RES(p) ((p) + 0x1C)
  46. /* WDTIM_INT bit definitions */
  47. #define MATCH_INT 1
  48. /* WDTIM_CTRL bit definitions */
  49. #define COUNT_ENAB 1
  50. #define RESET_COUNT (1<<1)
  51. #define DEBUG_EN (1<<2)
  52. /* WDTIM_MCTRL bit definitions */
  53. #define MR0_INT 1
  54. #undef RESET_COUNT0
  55. #define RESET_COUNT0 (1<<2)
  56. #define STOP_COUNT0 (1<<2)
  57. #define M_RES1 (1<<3)
  58. #define M_RES2 (1<<4)
  59. #define RESFRC1 (1<<5)
  60. #define RESFRC2 (1<<6)
  61. /* WDTIM_EMR bit definitions */
  62. #define EXT_MATCH0 1
  63. #define MATCH_OUTPUT_HIGH (2<<4) /*a MATCH_CTRL setting */
  64. /* WDTIM_RES bit definitions */
  65. #define WDOG_RESET 1 /* read only */
  66. #define WDOG_COUNTER_RATE 13000000 /*the counter clock is 13 MHz fixed */
  67. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  68. static int nowayout = 1;
  69. #else
  70. static int nowayout = 0;
  71. #endif
  72. static int heartbeat = DEFAULT_HEARTBEAT;
  73. static unsigned long wdt_status;
  74. #define WDT_IN_USE 0
  75. #define WDT_OK_TO_CLOSE 1
  76. #define WDT_REGION_INITED 2
  77. #define WDT_DEVICE_INITED 3
  78. static unsigned long boot_status;
  79. static struct resource *wdt_mem;
  80. static void __iomem *wdt_base;
  81. struct clk *wdt_clk;
  82. static void wdt_enable(void)
  83. {
  84. if (wdt_clk)
  85. clk_set_rate(wdt_clk, 1);
  86. /* stop counter, initiate counter reset */
  87. __raw_writel(RESET_COUNT, WDTIM_CTRL(wdt_base));
  88. /*wait for reset to complete. 100% guarantee event */
  89. while (__raw_readl(WDTIM_COUNTER(wdt_base)));
  90. /* internal and external reset, stop after that */
  91. __raw_writel(M_RES2 | STOP_COUNT0 | RESET_COUNT0,
  92. WDTIM_MCTRL(wdt_base));
  93. /* configure match output */
  94. __raw_writel(MATCH_OUTPUT_HIGH, WDTIM_EMR(wdt_base));
  95. /* clear interrupt, just in case */
  96. __raw_writel(MATCH_INT, WDTIM_INT(wdt_base));
  97. /* the longest pulse period 65541/(13*10^6) seconds ~ 5 ms. */
  98. __raw_writel(0xFFFF, WDTIM_PULSE(wdt_base));
  99. __raw_writel(heartbeat * WDOG_COUNTER_RATE, WDTIM_MATCH0(wdt_base));
  100. /*enable counter, stop when debugger active */
  101. __raw_writel(COUNT_ENAB | DEBUG_EN, WDTIM_CTRL(wdt_base));
  102. }
  103. static void wdt_disable(void)
  104. {
  105. __raw_writel(0, WDTIM_CTRL(wdt_base)); /*stop counter */
  106. if (wdt_clk)
  107. clk_set_rate(wdt_clk, 0);
  108. }
  109. static int pnx4008_wdt_open(struct inode *inode, struct file *file)
  110. {
  111. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  112. return -EBUSY;
  113. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  114. wdt_enable();
  115. return nonseekable_open(inode, file);
  116. }
  117. static ssize_t
  118. pnx4008_wdt_write(struct file *file, const char *data, size_t len,
  119. loff_t * ppos)
  120. {
  121. /* Can't seek (pwrite) on this device */
  122. if (ppos != &file->f_pos)
  123. return -ESPIPE;
  124. if (len) {
  125. if (!nowayout) {
  126. size_t i;
  127. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  128. for (i = 0; i != len; i++) {
  129. char c;
  130. if (get_user(c, data + i))
  131. return -EFAULT;
  132. if (c == 'V')
  133. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  134. }
  135. }
  136. wdt_enable();
  137. }
  138. return len;
  139. }
  140. static struct watchdog_info ident = {
  141. .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE |
  142. WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  143. .identity = "PNX4008 Watchdog",
  144. };
  145. static int
  146. pnx4008_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  147. unsigned long arg)
  148. {
  149. int ret = -ENOIOCTLCMD;
  150. int time;
  151. switch (cmd) {
  152. case WDIOC_GETSUPPORT:
  153. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  154. sizeof(ident)) ? -EFAULT : 0;
  155. break;
  156. case WDIOC_GETSTATUS:
  157. ret = put_user(0, (int *)arg);
  158. break;
  159. case WDIOC_GETBOOTSTATUS:
  160. ret = put_user(boot_status, (int *)arg);
  161. break;
  162. case WDIOC_SETTIMEOUT:
  163. ret = get_user(time, (int *)arg);
  164. if (ret)
  165. break;
  166. if (time <= 0 || time > MAX_HEARTBEAT) {
  167. ret = -EINVAL;
  168. break;
  169. }
  170. heartbeat = time;
  171. wdt_enable();
  172. /* Fall through */
  173. case WDIOC_GETTIMEOUT:
  174. ret = put_user(heartbeat, (int *)arg);
  175. break;
  176. case WDIOC_KEEPALIVE:
  177. wdt_enable();
  178. ret = 0;
  179. break;
  180. }
  181. return ret;
  182. }
  183. static int pnx4008_wdt_release(struct inode *inode, struct file *file)
  184. {
  185. if (!test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  186. printk(KERN_WARNING "WATCHDOG: Device closed unexpectdly\n");
  187. wdt_disable();
  188. clear_bit(WDT_IN_USE, &wdt_status);
  189. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  190. return 0;
  191. }
  192. static struct file_operations pnx4008_wdt_fops = {
  193. .owner = THIS_MODULE,
  194. .llseek = no_llseek,
  195. .write = pnx4008_wdt_write,
  196. .ioctl = pnx4008_wdt_ioctl,
  197. .open = pnx4008_wdt_open,
  198. .release = pnx4008_wdt_release,
  199. };
  200. static struct miscdevice pnx4008_wdt_miscdev = {
  201. .minor = WATCHDOG_MINOR,
  202. .name = "watchdog",
  203. .fops = &pnx4008_wdt_fops,
  204. };
  205. static int pnx4008_wdt_probe(struct platform_device *pdev)
  206. {
  207. int ret = 0, size;
  208. struct resource *res;
  209. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  210. heartbeat = DEFAULT_HEARTBEAT;
  211. printk(KERN_INFO MODULE_NAME
  212. "PNX4008 Watchdog Timer: heartbeat %d sec\n", heartbeat);
  213. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. if (res == NULL) {
  215. printk(KERN_INFO MODULE_NAME
  216. "failed to get memory region resouce\n");
  217. return -ENOENT;
  218. }
  219. size = res->end - res->start + 1;
  220. wdt_mem = request_mem_region(res->start, size, pdev->name);
  221. if (wdt_mem == NULL) {
  222. printk(KERN_INFO MODULE_NAME "failed to get memory region\n");
  223. return -ENOENT;
  224. }
  225. wdt_base = (void __iomem *)IO_ADDRESS(res->start);
  226. wdt_clk = clk_get(&pdev->dev, "wdt_ck");
  227. if (!wdt_clk) {
  228. release_resource(wdt_mem);
  229. kfree(wdt_mem);
  230. goto out;
  231. } else
  232. clk_set_rate(wdt_clk, 1);
  233. ret = misc_register(&pnx4008_wdt_miscdev);
  234. if (ret < 0) {
  235. printk(KERN_ERR MODULE_NAME "cannot register misc device\n");
  236. release_resource(wdt_mem);
  237. kfree(wdt_mem);
  238. clk_set_rate(wdt_clk, 0);
  239. } else {
  240. boot_status = (__raw_readl(WDTIM_RES(wdt_base)) & WDOG_RESET) ?
  241. WDIOF_CARDRESET : 0;
  242. wdt_disable(); /*disable for now */
  243. set_bit(WDT_DEVICE_INITED, &wdt_status);
  244. }
  245. out:
  246. return ret;
  247. }
  248. static int pnx4008_wdt_remove(struct platform_device *pdev)
  249. {
  250. if (wdt_mem) {
  251. release_resource(wdt_mem);
  252. kfree(wdt_mem);
  253. wdt_mem = NULL;
  254. }
  255. if (wdt_clk) {
  256. clk_set_rate(wdt_clk, 0);
  257. clk_put(wdt_clk);
  258. wdt_clk = NULL;
  259. }
  260. misc_deregister(&pnx4008_wdt_miscdev);
  261. return 0;
  262. }
  263. static struct platform_driver platform_wdt_driver = {
  264. .driver = {
  265. .name = "watchdog",
  266. },
  267. .probe = pnx4008_wdt_probe,
  268. .remove = pnx4008_wdt_remove,
  269. };
  270. static int __init pnx4008_wdt_init(void)
  271. {
  272. return platform_driver_register(&platform_wdt_driver);
  273. }
  274. static void __exit pnx4008_wdt_exit(void)
  275. {
  276. return platform_driver_unregister(&platform_wdt_driver);
  277. }
  278. module_init(pnx4008_wdt_init);
  279. module_exit(pnx4008_wdt_exit);
  280. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  281. MODULE_DESCRIPTION("PNX4008 Watchdog Driver");
  282. module_param(heartbeat, int, 0);
  283. MODULE_PARM_DESC(heartbeat,
  284. "Watchdog heartbeat period in seconds from 1 to "
  285. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  286. __MODULE_STRING(DEFAULT_HEARTBEAT));
  287. module_param(nowayout, int, 0);
  288. MODULE_PARM_DESC(nowayout,
  289. "Set to 1 to keep watchdog running after device release");
  290. MODULE_LICENSE("GPL");
  291. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);