orion5x_wdt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 orion5x_wdt_ping(void)
  41. {
  42. spin_lock(&wdt_lock);
  43. /* Reload watchdog duration */
  44. writel(wdt_tclk * heartbeat, WDT_VAL);
  45. spin_unlock(&wdt_lock);
  46. }
  47. static void orion5x_wdt_enable(void)
  48. {
  49. u32 reg;
  50. spin_lock(&wdt_lock);
  51. /* Set watchdog duration */
  52. writel(wdt_tclk * heartbeat, WDT_VAL);
  53. /* Clear watchdog timer interrupt */
  54. reg = readl(BRIDGE_CAUSE);
  55. reg &= ~WDT_INT_REQ;
  56. writel(reg, BRIDGE_CAUSE);
  57. /* Enable watchdog timer */
  58. reg = readl(TIMER_CTRL);
  59. reg |= WDT_EN;
  60. writel(reg, TIMER_CTRL);
  61. /* Enable reset on watchdog */
  62. reg = readl(CPU_RESET_MASK);
  63. reg |= WDT_RESET;
  64. writel(reg, CPU_RESET_MASK);
  65. spin_unlock(&wdt_lock);
  66. }
  67. static void orion5x_wdt_disable(void)
  68. {
  69. u32 reg;
  70. spin_lock(&wdt_lock);
  71. /* Disable reset on watchdog */
  72. reg = readl(CPU_RESET_MASK);
  73. reg &= ~WDT_RESET;
  74. writel(reg, CPU_RESET_MASK);
  75. /* Disable watchdog timer */
  76. reg = readl(TIMER_CTRL);
  77. reg &= ~WDT_EN;
  78. writel(reg, TIMER_CTRL);
  79. spin_unlock(&wdt_lock);
  80. }
  81. static int orion5x_wdt_get_timeleft(int *time_left)
  82. {
  83. spin_lock(&wdt_lock);
  84. *time_left = readl(WDT_VAL) / wdt_tclk;
  85. spin_unlock(&wdt_lock);
  86. return 0;
  87. }
  88. static int orion5x_wdt_open(struct inode *inode, struct file *file)
  89. {
  90. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  91. return -EBUSY;
  92. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  93. orion5x_wdt_enable();
  94. return nonseekable_open(inode, file);
  95. }
  96. static ssize_t orion5x_wdt_write(struct file *file, const char *data,
  97. size_t len, loff_t *ppos)
  98. {
  99. if (len) {
  100. if (!nowayout) {
  101. size_t i;
  102. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  103. for (i = 0; i != len; i++) {
  104. char c;
  105. if (get_user(c, data + i))
  106. return -EFAULT;
  107. if (c == 'V')
  108. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  109. }
  110. }
  111. orion5x_wdt_ping();
  112. }
  113. return len;
  114. }
  115. static int orion5x_wdt_settimeout(int new_time)
  116. {
  117. if ((new_time <= 0) || (new_time > wdt_max_duration))
  118. return -EINVAL;
  119. /* Set new watchdog time to be used when
  120. * orion5x_wdt_enable() or orion5x_wdt_ping() is called. */
  121. heartbeat = new_time;
  122. return 0;
  123. }
  124. static const struct watchdog_info ident = {
  125. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  126. WDIOF_KEEPALIVEPING,
  127. .identity = "Orion5x Watchdog",
  128. };
  129. static long orion5x_wdt_ioctl(struct file *file, unsigned int cmd,
  130. unsigned long arg)
  131. {
  132. int ret = -ENOTTY;
  133. int time;
  134. switch (cmd) {
  135. case WDIOC_GETSUPPORT:
  136. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  137. sizeof(ident)) ? -EFAULT : 0;
  138. break;
  139. case WDIOC_GETSTATUS:
  140. case WDIOC_GETBOOTSTATUS:
  141. ret = put_user(0, (int *)arg);
  142. break;
  143. case WDIOC_KEEPALIVE:
  144. orion5x_wdt_ping();
  145. ret = 0;
  146. break;
  147. case WDIOC_SETTIMEOUT:
  148. ret = get_user(time, (int *)arg);
  149. if (ret)
  150. break;
  151. if (orion5x_wdt_settimeout(time)) {
  152. ret = -EINVAL;
  153. break;
  154. }
  155. orion5x_wdt_ping();
  156. /* Fall through */
  157. case WDIOC_GETTIMEOUT:
  158. ret = put_user(heartbeat, (int *)arg);
  159. break;
  160. case WDIOC_GETTIMELEFT:
  161. if (orion5x_wdt_get_timeleft(&time)) {
  162. ret = -EINVAL;
  163. break;
  164. }
  165. ret = put_user(time, (int *)arg);
  166. break;
  167. }
  168. return ret;
  169. }
  170. static int orion5x_wdt_release(struct inode *inode, struct file *file)
  171. {
  172. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  173. orion5x_wdt_disable();
  174. else
  175. printk(KERN_CRIT "WATCHDOG: Device closed unexpectedly - "
  176. "timer will not stop\n");
  177. clear_bit(WDT_IN_USE, &wdt_status);
  178. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  179. return 0;
  180. }
  181. static const struct file_operations orion5x_wdt_fops = {
  182. .owner = THIS_MODULE,
  183. .llseek = no_llseek,
  184. .write = orion5x_wdt_write,
  185. .unlocked_ioctl = orion5x_wdt_ioctl,
  186. .open = orion5x_wdt_open,
  187. .release = orion5x_wdt_release,
  188. };
  189. static struct miscdevice orion5x_wdt_miscdev = {
  190. .minor = WATCHDOG_MINOR,
  191. .name = "watchdog",
  192. .fops = &orion5x_wdt_fops,
  193. };
  194. static int __devinit orion5x_wdt_probe(struct platform_device *pdev)
  195. {
  196. struct orion5x_wdt_platform_data *pdata = pdev->dev.platform_data;
  197. int ret;
  198. if (pdata) {
  199. wdt_tclk = pdata->tclk;
  200. } else {
  201. printk(KERN_ERR "Orion5x Watchdog misses platform data\n");
  202. return -ENODEV;
  203. }
  204. if (orion5x_wdt_miscdev.parent)
  205. return -EBUSY;
  206. orion5x_wdt_miscdev.parent = &pdev->dev;
  207. wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
  208. if (orion5x_wdt_settimeout(heartbeat))
  209. heartbeat = wdt_max_duration;
  210. ret = misc_register(&orion5x_wdt_miscdev);
  211. if (ret)
  212. return ret;
  213. printk(KERN_INFO "Orion5x Watchdog Timer: Initial timeout %d sec%s\n",
  214. heartbeat, nowayout ? ", nowayout" : "");
  215. return 0;
  216. }
  217. static int __devexit orion5x_wdt_remove(struct platform_device *pdev)
  218. {
  219. int ret;
  220. if (test_bit(WDT_IN_USE, &wdt_status)) {
  221. orion5x_wdt_disable();
  222. clear_bit(WDT_IN_USE, &wdt_status);
  223. }
  224. ret = misc_deregister(&orion5x_wdt_miscdev);
  225. if (!ret)
  226. orion5x_wdt_miscdev.parent = NULL;
  227. return ret;
  228. }
  229. static void orion5x_wdt_shutdown(struct platform_device *pdev)
  230. {
  231. if (test_bit(WDT_IN_USE, &wdt_status))
  232. orion5x_wdt_disable();
  233. }
  234. static struct platform_driver orion5x_wdt_driver = {
  235. .probe = orion5x_wdt_probe,
  236. .remove = __devexit_p(orion5x_wdt_remove),
  237. .shutdown = orion5x_wdt_shutdown,
  238. .driver = {
  239. .owner = THIS_MODULE,
  240. .name = "orion5x_wdt",
  241. },
  242. };
  243. static int __init orion5x_wdt_init(void)
  244. {
  245. spin_lock_init(&wdt_lock);
  246. return platform_driver_register(&orion5x_wdt_driver);
  247. }
  248. static void __exit orion5x_wdt_exit(void)
  249. {
  250. platform_driver_unregister(&orion5x_wdt_driver);
  251. }
  252. module_init(orion5x_wdt_init);
  253. module_exit(orion5x_wdt_exit);
  254. MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
  255. MODULE_DESCRIPTION("Orion5x Processor Watchdog");
  256. module_param(heartbeat, int, 0);
  257. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  258. module_param(nowayout, int, 0);
  259. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  260. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  261. MODULE_LICENSE("GPL");
  262. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);