orion_wdt.c 6.8 KB

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