orion_wdt.c 6.8 KB

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