orion_wdt.c 6.8 KB

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