mpc5200_wdt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <asm/of_platform.h>
  8. #include <asm/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 | GPT_MODE_MS_TIMER);
  50. spin_unlock(&wdt->io_lock);
  51. return 0;
  52. }
  53. static int mpc5200_wdt_ping(struct mpc5200_wdt *wdt)
  54. {
  55. spin_lock(&wdt->io_lock);
  56. /* writing A5 to OCPW resets the watchdog */
  57. out_be32(&wdt->regs->mode, 0xA5000000 | (0xffffff & in_be32(&wdt->regs->mode)));
  58. spin_unlock(&wdt->io_lock);
  59. return 0;
  60. }
  61. static int mpc5200_wdt_stop(struct mpc5200_wdt *wdt)
  62. {
  63. spin_lock(&wdt->io_lock);
  64. /* disable */
  65. out_be32(&wdt->regs->mode, 0);
  66. spin_unlock(&wdt->io_lock);
  67. return 0;
  68. }
  69. /* file operations */
  70. static ssize_t mpc5200_wdt_write(struct file *file, const char __user *data,
  71. size_t len, loff_t *ppos)
  72. {
  73. struct mpc5200_wdt *wdt = file->private_data;
  74. mpc5200_wdt_ping(wdt);
  75. return 0;
  76. }
  77. static struct watchdog_info mpc5200_wdt_info = {
  78. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  79. .identity = "mpc5200 watchdog on GPT0",
  80. };
  81. static int mpc5200_wdt_ioctl(struct inode *inode, struct file *file,
  82. unsigned int cmd, unsigned long arg)
  83. {
  84. struct mpc5200_wdt *wdt = file->private_data;
  85. int __user *data = (int __user *)arg;
  86. int timeout;
  87. int ret = 0;
  88. switch (cmd) {
  89. case WDIOC_GETSUPPORT:
  90. ret = copy_to_user(data, &mpc5200_wdt_info,
  91. sizeof(mpc5200_wdt_info));
  92. if (ret)
  93. ret = -EFAULT;
  94. break;
  95. case WDIOC_GETSTATUS:
  96. case WDIOC_GETBOOTSTATUS:
  97. ret = put_user(0, data);
  98. break;
  99. case WDIOC_KEEPALIVE:
  100. mpc5200_wdt_ping(wdt);
  101. break;
  102. case WDIOC_SETTIMEOUT:
  103. ret = get_user(timeout, data);
  104. if (ret)
  105. break;
  106. mpc5200_wdt_set_timeout(wdt, timeout);
  107. mpc5200_wdt_start(wdt);
  108. /* fall through and return the timeout */
  109. case WDIOC_GETTIMEOUT:
  110. timeout = mpc5200_wdt_get_timeout(wdt);
  111. ret = put_user(timeout, data);
  112. break;
  113. default:
  114. ret = -ENOTTY;
  115. }
  116. return ret;
  117. }
  118. static int mpc5200_wdt_open(struct inode *inode, struct file *file)
  119. {
  120. /* /dev/watchdog can only be opened once */
  121. if (test_and_set_bit(0, &is_active))
  122. return -EBUSY;
  123. /* Set and activate the watchdog */
  124. mpc5200_wdt_set_timeout(wdt_global, 30);
  125. mpc5200_wdt_start(wdt_global);
  126. file->private_data = wdt_global;
  127. return nonseekable_open(inode, file);
  128. }
  129. static int mpc5200_wdt_release(struct inode *inode, struct file *file)
  130. {
  131. #if WATCHDOG_NOWAYOUT == 0
  132. struct mpc5200_wdt *wdt = file->private_data;
  133. mpc5200_wdt_stop(wdt);
  134. wdt->count = 0; /* == disabled */
  135. #endif
  136. clear_bit(0, &is_active);
  137. return 0;
  138. }
  139. static struct file_operations mpc5200_wdt_fops = {
  140. .owner = THIS_MODULE,
  141. .write = mpc5200_wdt_write,
  142. .ioctl = mpc5200_wdt_ioctl,
  143. .open = mpc5200_wdt_open,
  144. .release = mpc5200_wdt_release,
  145. };
  146. /* module operations */
  147. static int mpc5200_wdt_probe(struct of_device *op, const struct of_device_id *match)
  148. {
  149. struct mpc5200_wdt *wdt;
  150. int err;
  151. const void *has_wdt;
  152. int size;
  153. has_wdt = of_get_property(op->node, "has-wdt", NULL);
  154. if (!has_wdt)
  155. return -ENODEV;
  156. wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
  157. if (!wdt)
  158. return -ENOMEM;
  159. wdt->ipb_freq = mpc52xx_find_ipb_freq(op->node);
  160. err = of_address_to_resource(op->node, 0, &wdt->mem);
  161. if (err)
  162. goto out_free;
  163. size = wdt->mem.end - wdt->mem.start + 1;
  164. if (!request_mem_region(wdt->mem.start, size, "mpc5200_wdt")) {
  165. err = -ENODEV;
  166. goto out_free;
  167. }
  168. wdt->regs = ioremap(wdt->mem.start, size);
  169. if (!wdt->regs) {
  170. err = -ENODEV;
  171. goto out_release;
  172. }
  173. dev_set_drvdata(&op->dev, wdt);
  174. spin_lock_init(&wdt->io_lock);
  175. wdt->miscdev = (struct miscdevice) {
  176. .minor = WATCHDOG_MINOR,
  177. .name = "watchdog",
  178. .fops = &mpc5200_wdt_fops,
  179. .parent = &op->dev,
  180. };
  181. wdt_global = wdt;
  182. err = misc_register(&wdt->miscdev);
  183. if (!err)
  184. return 0;
  185. iounmap(wdt->regs);
  186. out_release:
  187. release_mem_region(wdt->mem.start, size);
  188. out_free:
  189. kfree(wdt);
  190. return err;
  191. }
  192. static int mpc5200_wdt_remove(struct of_device *op)
  193. {
  194. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  195. mpc5200_wdt_stop(wdt);
  196. misc_deregister(&wdt->miscdev);
  197. iounmap(wdt->regs);
  198. release_mem_region(wdt->mem.start, wdt->mem.end - wdt->mem.start + 1);
  199. kfree(wdt);
  200. return 0;
  201. }
  202. static int mpc5200_wdt_suspend(struct of_device *op, pm_message_t state)
  203. {
  204. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  205. mpc5200_wdt_stop(wdt);
  206. return 0;
  207. }
  208. static int mpc5200_wdt_resume(struct of_device *op)
  209. {
  210. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  211. if (wdt->count)
  212. mpc5200_wdt_start(wdt);
  213. return 0;
  214. }
  215. static int mpc5200_wdt_shutdown(struct of_device *op)
  216. {
  217. struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
  218. mpc5200_wdt_stop(wdt);
  219. return 0;
  220. }
  221. static struct of_device_id mpc5200_wdt_match[] = {
  222. { .compatible = "mpc5200-gpt", },
  223. {},
  224. };
  225. static struct of_platform_driver mpc5200_wdt_driver = {
  226. .owner = THIS_MODULE,
  227. .name = "mpc5200-gpt-wdt",
  228. .match_table = mpc5200_wdt_match,
  229. .probe = mpc5200_wdt_probe,
  230. .remove = mpc5200_wdt_remove,
  231. .suspend = mpc5200_wdt_suspend,
  232. .resume = mpc5200_wdt_resume,
  233. .shutdown = mpc5200_wdt_shutdown,
  234. };
  235. static int __init mpc5200_wdt_init(void)
  236. {
  237. return of_register_platform_driver(&mpc5200_wdt_driver);
  238. }
  239. static void __exit mpc5200_wdt_exit(void)
  240. {
  241. of_unregister_platform_driver(&mpc5200_wdt_driver);
  242. }
  243. module_init(mpc5200_wdt_init);
  244. module_exit(mpc5200_wdt_exit);
  245. MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>");
  246. MODULE_LICENSE("Dual BSD/GPL");
  247. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);