orion_wdt.c 7.1 KB

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