orion5x_wdt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * drivers/watchdog/orion5x_wdt.c
  3. *
  4. * Watchdog driver for Orion5x processors
  5. *
  6. * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/fs.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/init.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/io.h>
  23. #include <linux/spinlock.h>
  24. #include <plat/orion5x_wdt.h>
  25. /*
  26. * Watchdog timer block registers.
  27. */
  28. #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
  29. #define WDT_EN 0x0010
  30. #define WDT_VAL (TIMER_VIRT_BASE + 0x0024)
  31. #define WDT_MAX_CYCLE_COUNT 0xffffffff
  32. #define WDT_IN_USE 0
  33. #define WDT_OK_TO_CLOSE 1
  34. static int nowayout = WATCHDOG_NOWAYOUT;
  35. static int heartbeat = -1; /* module parameter (seconds) */
  36. static unsigned int wdt_max_duration; /* (seconds) */
  37. static unsigned int wdt_tclk;
  38. static unsigned long wdt_status;
  39. static spinlock_t wdt_lock;
  40. static void wdt_enable(void)
  41. {
  42. u32 reg;
  43. spin_lock(&wdt_lock);
  44. /* Set watchdog duration */
  45. writel(wdt_tclk * heartbeat, WDT_VAL);
  46. /* Clear watchdog timer interrupt */
  47. reg = readl(BRIDGE_CAUSE);
  48. reg &= ~WDT_INT_REQ;
  49. writel(reg, BRIDGE_CAUSE);
  50. /* Enable watchdog timer */
  51. reg = readl(TIMER_CTRL);
  52. reg |= WDT_EN;
  53. writel(reg, TIMER_CTRL);
  54. /* Enable reset on watchdog */
  55. reg = readl(CPU_RESET_MASK);
  56. reg |= WDT_RESET;
  57. writel(reg, CPU_RESET_MASK);
  58. spin_unlock(&wdt_lock);
  59. }
  60. static void wdt_disable(void)
  61. {
  62. u32 reg;
  63. spin_lock(&wdt_lock);
  64. /* Disable reset on watchdog */
  65. reg = readl(CPU_RESET_MASK);
  66. reg &= ~WDT_RESET;
  67. writel(reg, CPU_RESET_MASK);
  68. /* Disable watchdog timer */
  69. reg = readl(TIMER_CTRL);
  70. reg &= ~WDT_EN;
  71. writel(reg, TIMER_CTRL);
  72. spin_unlock(&wdt_lock);
  73. }
  74. static int orion5x_wdt_get_timeleft(int *time_left)
  75. {
  76. spin_lock(&wdt_lock);
  77. *time_left = readl(WDT_VAL) / wdt_tclk;
  78. spin_unlock(&wdt_lock);
  79. return 0;
  80. }
  81. static int orion5x_wdt_open(struct inode *inode, struct file *file)
  82. {
  83. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  84. return -EBUSY;
  85. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  86. wdt_enable();
  87. return nonseekable_open(inode, file);
  88. }
  89. static ssize_t orion5x_wdt_write(struct file *file, const char *data,
  90. size_t len, loff_t *ppos)
  91. {
  92. if (len) {
  93. if (!nowayout) {
  94. size_t i;
  95. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  96. for (i = 0; i != len; i++) {
  97. char c;
  98. if (get_user(c, data + i))
  99. return -EFAULT;
  100. if (c == 'V')
  101. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  102. }
  103. }
  104. wdt_enable();
  105. }
  106. return len;
  107. }
  108. static struct watchdog_info ident = {
  109. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  110. WDIOF_KEEPALIVEPING,
  111. .identity = "Orion5x Watchdog",
  112. };
  113. static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd,
  114. unsigned long arg)
  115. {
  116. int ret = -ENOTTY;
  117. int time;
  118. switch (cmd) {
  119. case WDIOC_GETSUPPORT:
  120. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  121. sizeof(ident)) ? -EFAULT : 0;
  122. break;
  123. case WDIOC_GETSTATUS:
  124. case WDIOC_GETBOOTSTATUS:
  125. ret = put_user(0, (int *)arg);
  126. break;
  127. case WDIOC_KEEPALIVE:
  128. wdt_enable();
  129. ret = 0;
  130. break;
  131. case WDIOC_SETTIMEOUT:
  132. ret = get_user(time, (int *)arg);
  133. if (ret)
  134. break;
  135. if (time <= 0 || time > wdt_max_duration) {
  136. ret = -EINVAL;
  137. break;
  138. }
  139. heartbeat = time;
  140. wdt_enable();
  141. /* Fall through */
  142. case WDIOC_GETTIMEOUT:
  143. ret = put_user(heartbeat, (int *)arg);
  144. break;
  145. case WDIOC_GETTIMELEFT:
  146. if (orion5x_wdt_get_timeleft(&time)) {
  147. ret = -EINVAL;
  148. break;
  149. }
  150. ret = put_user(time, (int *)arg);
  151. break;
  152. }
  153. return ret;
  154. }
  155. static int orion5x_wdt_release(struct inode *inode, struct file *file)
  156. {
  157. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  158. wdt_disable();
  159. else
  160. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  161. "timer will not stop\n");
  162. clear_bit(WDT_IN_USE, &wdt_status);
  163. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  164. return 0;
  165. }
  166. static const struct file_operations orion5x_wdt_fops = {
  167. .owner = THIS_MODULE,
  168. .llseek = no_llseek,
  169. .write = orion5x_wdt_write,
  170. .unlocked_ioctl = orion5x_wdt_ioctl,
  171. .open = orion5x_wdt_open,
  172. .release = orion5x_wdt_release,
  173. };
  174. static struct miscdevice orion5x_wdt_miscdev = {
  175. .minor = WATCHDOG_MINOR,
  176. .name = "watchdog",
  177. .fops = &orion5x_wdt_fops,
  178. };
  179. static int __devinit orion5x_wdt_probe(struct platform_device *pdev)
  180. {
  181. struct orion5x_wdt_platform_data *pdata = pdev->dev.platform_data;
  182. int ret;
  183. if (pdata) {
  184. wdt_tclk = pdata->tclk;
  185. } else {
  186. printk(KERN_ERR "Orion5x Watchdog misses platform data\n");
  187. return -ENODEV;
  188. }
  189. if (orion5x_wdt_miscdev.parent)
  190. return -EBUSY;
  191. orion5x_wdt_miscdev.parent = &pdev->dev;
  192. wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
  193. if (heartbeat <= 0 || heartbeat > wdt_max_duration)
  194. heartbeat = wdt_max_duration;
  195. ret = misc_register(&orion5x_wdt_miscdev);
  196. if (ret)
  197. return ret;
  198. printk(KERN_INFO "Orion5x Watchdog Timer: Initial timeout %d sec%s\n",
  199. heartbeat, nowayout ? ", nowayout" : "");
  200. return 0;
  201. }
  202. static int __devexit orion5x_wdt_remove(struct platform_device *pdev)
  203. {
  204. int ret;
  205. if (test_bit(WDT_IN_USE, &wdt_status)) {
  206. wdt_disable();
  207. clear_bit(WDT_IN_USE, &wdt_status);
  208. }
  209. ret = misc_deregister(&orion5x_wdt_miscdev);
  210. if (!ret)
  211. orion5x_wdt_miscdev.parent = NULL;
  212. return ret;
  213. }
  214. static struct platform_driver orion5x_wdt_driver = {
  215. .probe = orion5x_wdt_probe,
  216. .remove = __devexit_p(orion5x_wdt_remove),
  217. .driver = {
  218. .owner = THIS_MODULE,
  219. .name = "orion5x_wdt",
  220. },
  221. };
  222. static int __init orion5x_wdt_init(void)
  223. {
  224. spin_lock_init(&wdt_lock);
  225. return platform_driver_register(&orion5x_wdt_driver);
  226. }
  227. static void __exit orion5x_wdt_exit(void)
  228. {
  229. platform_driver_unregister(&orion5x_wdt_driver);
  230. }
  231. module_init(orion5x_wdt_init);
  232. module_exit(orion5x_wdt_exit);
  233. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  234. MODULE_DESCRIPTION("Orion5x Processor Watchdog");
  235. module_param(heartbeat, int, 0);
  236. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
  237. module_param(nowayout, int, 0);
  238. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  239. MODULE_LICENSE("GPL");
  240. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);