orion_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/init.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/io.h>
  24. #include <linux/spinlock.h>
  25. #include <mach/bridge-regs.h>
  26. #include <plat/orion_wdt.h>
  27. /*
  28. * Watchdog timer block registers.
  29. */
  30. #define TIMER_CTRL 0x0000
  31. #define WDT_EN 0x0010
  32. #define WDT_VAL 0x0024
  33. #define WDT_MAX_CYCLE_COUNT 0xffffffff
  34. #define WDT_IN_USE 0
  35. #define WDT_OK_TO_CLOSE 1
  36. static bool nowayout = WATCHDOG_NOWAYOUT;
  37. static int heartbeat = -1; /* module parameter (seconds) */
  38. static unsigned int wdt_max_duration; /* (seconds) */
  39. static unsigned int wdt_tclk;
  40. static void __iomem *wdt_reg;
  41. static unsigned long wdt_status;
  42. static DEFINE_SPINLOCK(wdt_lock);
  43. static void orion_wdt_ping(void)
  44. {
  45. spin_lock(&wdt_lock);
  46. /* Reload watchdog duration */
  47. writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
  48. spin_unlock(&wdt_lock);
  49. }
  50. static void orion_wdt_enable(void)
  51. {
  52. u32 reg;
  53. spin_lock(&wdt_lock);
  54. /* Set watchdog duration */
  55. writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL);
  56. /* Clear watchdog timer interrupt */
  57. reg = readl(BRIDGE_CAUSE);
  58. reg &= ~WDT_INT_REQ;
  59. writel(reg, BRIDGE_CAUSE);
  60. /* Enable watchdog timer */
  61. reg = readl(wdt_reg + TIMER_CTRL);
  62. reg |= WDT_EN;
  63. writel(reg, wdt_reg + TIMER_CTRL);
  64. /* Enable reset on watchdog */
  65. reg = readl(RSTOUTn_MASK);
  66. reg |= WDT_RESET_OUT_EN;
  67. writel(reg, RSTOUTn_MASK);
  68. spin_unlock(&wdt_lock);
  69. }
  70. static void orion_wdt_disable(void)
  71. {
  72. u32 reg;
  73. spin_lock(&wdt_lock);
  74. /* Disable reset on watchdog */
  75. reg = readl(RSTOUTn_MASK);
  76. reg &= ~WDT_RESET_OUT_EN;
  77. writel(reg, RSTOUTn_MASK);
  78. /* Disable watchdog timer */
  79. reg = readl(wdt_reg + TIMER_CTRL);
  80. reg &= ~WDT_EN;
  81. writel(reg, wdt_reg + TIMER_CTRL);
  82. spin_unlock(&wdt_lock);
  83. }
  84. static int orion_wdt_get_timeleft(int *time_left)
  85. {
  86. spin_lock(&wdt_lock);
  87. *time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
  88. spin_unlock(&wdt_lock);
  89. return 0;
  90. }
  91. static int orion_wdt_open(struct inode *inode, struct file *file)
  92. {
  93. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  94. return -EBUSY;
  95. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  96. orion_wdt_enable();
  97. return nonseekable_open(inode, file);
  98. }
  99. static ssize_t orion_wdt_write(struct file *file, const char *data,
  100. size_t len, loff_t *ppos)
  101. {
  102. if (len) {
  103. if (!nowayout) {
  104. size_t i;
  105. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  106. for (i = 0; i != len; i++) {
  107. char c;
  108. if (get_user(c, data + i))
  109. return -EFAULT;
  110. if (c == 'V')
  111. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  112. }
  113. }
  114. orion_wdt_ping();
  115. }
  116. return len;
  117. }
  118. static int orion_wdt_settimeout(int new_time)
  119. {
  120. if ((new_time <= 0) || (new_time > wdt_max_duration))
  121. return -EINVAL;
  122. /* Set new watchdog time to be used when
  123. * orion_wdt_enable() or orion_wdt_ping() is called. */
  124. heartbeat = new_time;
  125. return 0;
  126. }
  127. static const struct watchdog_info ident = {
  128. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  129. WDIOF_KEEPALIVEPING,
  130. .identity = "Orion Watchdog",
  131. };
  132. static long orion_wdt_ioctl(struct file *file, unsigned int cmd,
  133. unsigned long arg)
  134. {
  135. int ret = -ENOTTY;
  136. int time;
  137. switch (cmd) {
  138. case WDIOC_GETSUPPORT:
  139. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  140. sizeof(ident)) ? -EFAULT : 0;
  141. break;
  142. case WDIOC_GETSTATUS:
  143. case WDIOC_GETBOOTSTATUS:
  144. ret = put_user(0, (int *)arg);
  145. break;
  146. case WDIOC_KEEPALIVE:
  147. orion_wdt_ping();
  148. ret = 0;
  149. break;
  150. case WDIOC_SETTIMEOUT:
  151. ret = get_user(time, (int *)arg);
  152. if (ret)
  153. break;
  154. if (orion_wdt_settimeout(time)) {
  155. ret = -EINVAL;
  156. break;
  157. }
  158. orion_wdt_ping();
  159. /* Fall through */
  160. case WDIOC_GETTIMEOUT:
  161. ret = put_user(heartbeat, (int *)arg);
  162. break;
  163. case WDIOC_GETTIMELEFT:
  164. if (orion_wdt_get_timeleft(&time)) {
  165. ret = -EINVAL;
  166. break;
  167. }
  168. ret = put_user(time, (int *)arg);
  169. break;
  170. }
  171. return ret;
  172. }
  173. static int orion_wdt_release(struct inode *inode, struct file *file)
  174. {
  175. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  176. orion_wdt_disable();
  177. else
  178. pr_crit("Device closed unexpectedly - 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. pr_err("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. pr_info("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, bool, 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);