mpc5200_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/miscdevice.h>
  4. #include <linux/watchdog.h>
  5. #include <linux/io.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/of_platform.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/mpc52xx.h>
  10. #define GPT_MODE_WDT (1<<15)
  11. #define GPT_MODE_CE (1<<12)
  12. #define GPT_MODE_MS_TIMER (0x4)
  13. struct mpc5200_wdt {
  14. unsigned count; /* timer ticks before watchdog kicks in */
  15. long ipb_freq;
  16. struct miscdevice miscdev;
  17. struct resource mem;
  18. struct mpc52xx_gpt __iomem *regs;
  19. spinlock_t io_lock;
  20. };
  21. /* is_active stores wether or not the /dev/watchdog device is opened */
  22. static unsigned long is_active;
  23. /* misc devices don't provide a way, to get back to 'dev' or 'miscdev' from
  24. * file operations, which sucks. But there can be max 1 watchdog anyway, so...
  25. */
  26. static struct mpc5200_wdt *wdt_global;
  27. /* helper to calculate timeout in timer counts */
  28. static void mpc5200_wdt_set_timeout(struct mpc5200_wdt *wdt, int timeout)
  29. {
  30. /* use biggest prescaler of 64k */
  31. wdt->count = (wdt->ipb_freq + 0xffff) / 0x10000 * timeout;
  32. if (wdt->count > 0xffff)
  33. wdt->count = 0xffff;
  34. }
  35. /* return timeout in seconds (calculated from timer count) */
  36. static int mpc5200_wdt_get_timeout(struct mpc5200_wdt *wdt)
  37. {
  38. return wdt->count * 0x10000 / wdt->ipb_freq;
  39. }
  40. /* watchdog operations */
  41. static int mpc5200_wdt_start(struct mpc5200_wdt *wdt)
  42. {
  43. spin_lock(&wdt->io_lock);
  44. /* disable */
  45. out_be32(&wdt->regs->mode, 0);
  46. /* set timeout, with maximum prescaler */
  47. out_be32(&wdt->regs->count, 0x0 | wdt->count);
  48. /* enable watchdog */
  49. out_be32(&wdt->regs->mode, GPT_MODE_CE | GPT_MODE_WDT |
  50. GPT_MODE_MS_TIMER);
  51. spin_unlock(&wdt->io_lock);
  52. return 0;
  53. }
  54. static int mpc5200_wdt_ping(struct mpc5200_wdt *wdt)
  55. {
  56. spin_lock(&wdt->io_lock);
  57. /* writing A5 to OCPW resets the watchdog */
  58. out_be32(&wdt->regs->mode, 0xA5000000 |
  59. (0xffffff & in_be32(&wdt->regs->mode)));
  60. spin_unlock(&wdt->io_lock);
  61. return 0;
  62. }
  63. static int mpc5200_wdt_stop(struct mpc5200_wdt *wdt)
  64. {
  65. spin_lock(&wdt->io_lock);
  66. /* disable */
  67. out_be32(&wdt->regs->mode, 0);
  68. spin_unlock(&wdt->io_lock);
  69. return 0;
  70. }
  71. /* file operations */
  72. static ssize_t mpc5200_wdt_write(struct file *file, const char __user *data,
  73. size_t len, loff_t *ppos)
  74. {
  75. struct mpc5200_wdt *wdt = file->private_data;
  76. mpc5200_wdt_ping(wdt);
  77. return 0;
  78. }
  79. static struct watchdog_info mpc5200_wdt_info = {
  80. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  81. .identity = "mpc5200 watchdog on GPT0",
  82. };
  83. static long mpc5200_wdt_ioctl(struct file *file, unsigned int cmd,
  84. unsigned long arg)
  85. {
  86. struct mpc5200_wdt *wdt = file->private_data;
  87. int __user *data = (int __user *)arg;
  88. int timeout;
  89. int ret = 0;
  90. switch (cmd) {
  91. case WDIOC_GETSUPPORT:
  92. ret = copy_to_user(data, &mpc5200_wdt_info,
  93. sizeof(mpc5200_wdt_info));
  94. if (ret)
  95. ret = -EFAULT;
  96. break;
  97. case WDIOC_GETSTATUS:
  98. case WDIOC_GETBOOTSTATUS:
  99. ret = put_user(0, data);
  100. break;
  101. case WDIOC_KEEPALIVE:
  102. mpc5200_wdt_ping(wdt);
  103. break;
  104. case WDIOC_SETTIMEOUT:
  105. ret = get_user(timeout, data);
  106. if (ret)
  107. break;
  108. mpc5200_wdt_set_timeout(wdt, timeout);
  109. mpc5200_wdt_start(wdt);
  110. /* fall through and return the timeout */
  111. case WDIOC_GETTIMEOUT:
  112. timeout = mpc5200_wdt_get_timeout(wdt);
  113. ret = put_user(timeout, data);
  114. break;
  115. default:
  116. ret = -ENOTTY;
  117. }
  118. return ret;
  119. }
  120. static int mpc5200_wdt_open(struct inode *inode, struct file *file)
  121. {
  122. /* /dev/watchdog can only be opened once */
  123. if (test_and_set_bit(0, &is_active))
  124. return -EBUSY;
  125. /* Set and activate the watchdog */
  126. mpc5200_wdt_set_timeout(wdt_global, 30);
  127. mpc5200_wdt_start(wdt_global);
  128. file->private_data = wdt_global;
  129. return nonseekable_open(inode, file);
  130. }
  131. static int mpc5200_wdt_release(struct inode *inode, struct file *file)
  132. {
  133. #if WATCHDOG_NOWAYOUT == 0
  134. struct mpc5200_wdt *wdt = file->private_data;
  135. mpc5200_wdt_stop(wdt);
  136. wdt->count = 0; /* == disabled */
  137. #endif
  138. clear_bit(0, &is_active);
  139. return 0;
  140. }
  141. static const struct file_operations mpc5200_wdt_fops = {
  142. .owner = THIS_MODULE,
  143. .write = mpc5200_wdt_write,
  144. .unlocked_ioctl = mpc5200_wdt_ioctl,
  145. .open = mpc5200_wdt_open,
  146. .release = mpc5200_wdt_release,
  147. };
  148. /* module operations */
  149. static int mpc5200_wdt_probe(struct of_device *op,
  150. const struct of_device_id *match)
  151. {
  152. struct mpc5200_wdt *wdt;
  153. int err;
  154. const void *has_wdt;
  155. int size;
  156. has_wdt = of_get_property(op->node, "has-wdt", NULL);
  157. if (!has_wdt)
  158. has_wdt = of_get_property(op->node, "fsl,has-wdt", NULL);
  159. if (!has_wdt)
  160. return -ENODEV;
  161. wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
  162. if (!wdt)
  163. return -ENOMEM;
  164. wdt->ipb_freq = mpc52xx_find_ipb_freq(op->node);
  165. err = of_address_to_resource(op->node, 0, &wdt->mem);
  166. if (err)
  167. goto out_free;
  168. size = wdt->mem.end - wdt->mem.start + 1;
  169. if (!request_mem_region(wdt->mem.start, size, "mpc5200_wdt")) {
  170. err = -ENODEV;
  171. goto out_free;
  172. }
  173. wdt->regs = ioremap(wdt->mem.start, size);
  174. if (!wdt->regs) {
  175. err = -ENODEV;
  176. goto out_release;
  177. }
  178. dev_set_drvdata(&op->dev, wdt);
  179. spin_lock_init(&wdt->io_lock);
  180. wdt->miscdev = (struct miscdevice) {
  181. .minor = WATCHDOG_MINOR,
  182. .name = "watchdog",
  183. .fops = &mpc5200_wdt_fops,
  184. .parent = &op->dev,
  185. };
  186. wdt_global = wdt;
  187. err = misc_register(&wdt->miscdev);
  188. if (!err)
  189. return 0;
  190. iounmap(wdt->regs);
  191. out_release:
  192. release_mem_region(wdt->mem.start, size);
  193. out_free:
  194. kfree(wdt);
  195. return err;
  196. }
  197. static int mpc5200_wdt_remove(struct of_device *op)
  198. {
  199. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  200. mpc5200_wdt_stop(wdt);
  201. misc_deregister(&wdt->miscdev);
  202. iounmap(wdt->regs);
  203. release_mem_region(wdt->mem.start, wdt->mem.end - wdt->mem.start + 1);
  204. kfree(wdt);
  205. return 0;
  206. }
  207. static int mpc5200_wdt_suspend(struct of_device *op, pm_message_t state)
  208. {
  209. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  210. mpc5200_wdt_stop(wdt);
  211. return 0;
  212. }
  213. static int mpc5200_wdt_resume(struct of_device *op)
  214. {
  215. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  216. if (wdt->count)
  217. mpc5200_wdt_start(wdt);
  218. return 0;
  219. }
  220. static int mpc5200_wdt_shutdown(struct of_device *op)
  221. {
  222. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  223. mpc5200_wdt_stop(wdt);
  224. return 0;
  225. }
  226. static struct of_device_id mpc5200_wdt_match[] = {
  227. { .compatible = "mpc5200-gpt", },
  228. { .compatible = "fsl,mpc5200-gpt", },
  229. {},
  230. };
  231. static struct of_platform_driver mpc5200_wdt_driver = {
  232. .owner = THIS_MODULE,
  233. .name = "mpc5200-gpt-wdt",
  234. .match_table = mpc5200_wdt_match,
  235. .probe = mpc5200_wdt_probe,
  236. .remove = mpc5200_wdt_remove,
  237. .suspend = mpc5200_wdt_suspend,
  238. .resume = mpc5200_wdt_resume,
  239. .shutdown = mpc5200_wdt_shutdown,
  240. };
  241. static int __init mpc5200_wdt_init(void)
  242. {
  243. return of_register_platform_driver(&mpc5200_wdt_driver);
  244. }
  245. static void __exit mpc5200_wdt_exit(void)
  246. {
  247. of_unregister_platform_driver(&mpc5200_wdt_driver);
  248. }
  249. module_init(mpc5200_wdt_init);
  250. module_exit(mpc5200_wdt_exit);
  251. MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>");
  252. MODULE_LICENSE("Dual BSD/GPL");
  253. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);