gemini_wdt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Watchdog driver for Cortina Systems Gemini SoC
  3. *
  4. * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/fs.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/slab.h>
  20. #define GEMINI_WDCOUNTER 0x0
  21. #define GEMINI_WDLOAD 0x4
  22. #define GEMINI_WDRESTART 0x8
  23. #define WDRESTART_MAGIC 0x5AB9
  24. #define GEMINI_WDCR 0xC
  25. #define WDCR_CLOCK_5MHZ (1 << 4)
  26. #define WDCR_SYS_RST (1 << 1)
  27. #define WDCR_ENABLE (1 << 0)
  28. #define WDT_CLOCK 5000000 /* 5 MHz */
  29. #define WDT_DEFAULT_TIMEOUT 13
  30. #define WDT_MAX_TIMEOUT (0xFFFFFFFF / WDT_CLOCK)
  31. /* status bits */
  32. #define WDT_ACTIVE 0
  33. #define WDT_OK_TO_CLOSE 1
  34. static unsigned int timeout = WDT_DEFAULT_TIMEOUT;
  35. static int nowayout = WATCHDOG_NOWAYOUT;
  36. static DEFINE_SPINLOCK(gemini_wdt_lock);
  37. static struct platform_device *gemini_wdt_dev;
  38. struct gemini_wdt_struct {
  39. struct resource *res;
  40. struct device *dev;
  41. void __iomem *base;
  42. unsigned long status;
  43. };
  44. static struct watchdog_info gemini_wdt_info = {
  45. .identity = "Gemini watchdog",
  46. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
  47. WDIOF_SETTIMEOUT,
  48. };
  49. /* Disable the watchdog. */
  50. static void gemini_wdt_stop(struct gemini_wdt_struct *gemini_wdt)
  51. {
  52. spin_lock(&gemini_wdt_lock);
  53. __raw_writel(0, gemini_wdt->base + GEMINI_WDCR);
  54. clear_bit(WDT_ACTIVE, &gemini_wdt->status);
  55. spin_unlock(&gemini_wdt_lock);
  56. }
  57. /* Service the watchdog */
  58. static void gemini_wdt_service(struct gemini_wdt_struct *gemini_wdt)
  59. {
  60. __raw_writel(WDRESTART_MAGIC, gemini_wdt->base + GEMINI_WDRESTART);
  61. }
  62. /* Enable and reset the watchdog. */
  63. static void gemini_wdt_start(struct gemini_wdt_struct *gemini_wdt)
  64. {
  65. spin_lock(&gemini_wdt_lock);
  66. __raw_writel(timeout * WDT_CLOCK, gemini_wdt->base + GEMINI_WDLOAD);
  67. gemini_wdt_service(gemini_wdt);
  68. /* set clock before enabling */
  69. __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST,
  70. gemini_wdt->base + GEMINI_WDCR);
  71. __raw_writel(WDCR_CLOCK_5MHZ | WDCR_SYS_RST | WDCR_ENABLE,
  72. gemini_wdt->base + GEMINI_WDCR);
  73. set_bit(WDT_ACTIVE, &gemini_wdt->status);
  74. spin_unlock(&gemini_wdt_lock);
  75. }
  76. /* Watchdog device is opened, and watchdog starts running. */
  77. static int gemini_wdt_open(struct inode *inode, struct file *file)
  78. {
  79. struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(gemini_wdt_dev);
  80. if (test_bit(WDT_ACTIVE, &gemini_wdt->status))
  81. return -EBUSY;
  82. file->private_data = gemini_wdt;
  83. gemini_wdt_start(gemini_wdt);
  84. return nonseekable_open(inode, file);
  85. }
  86. /* Close the watchdog device. */
  87. static int gemini_wdt_close(struct inode *inode, struct file *file)
  88. {
  89. struct gemini_wdt_struct *gemini_wdt = file->private_data;
  90. /* Disable the watchdog if possible */
  91. if (test_bit(WDT_OK_TO_CLOSE, &gemini_wdt->status))
  92. gemini_wdt_stop(gemini_wdt);
  93. else
  94. dev_warn(gemini_wdt->dev, "Device closed unexpectedly - timer will not stop\n");
  95. return 0;
  96. }
  97. /* Handle commands from user-space. */
  98. static long gemini_wdt_ioctl(struct file *file, unsigned int cmd,
  99. unsigned long arg)
  100. {
  101. struct gemini_wdt_struct *gemini_wdt = file->private_data;
  102. int value;
  103. switch (cmd) {
  104. case WDIOC_KEEPALIVE:
  105. gemini_wdt_service(gemini_wdt);
  106. return 0;
  107. case WDIOC_GETSUPPORT:
  108. return copy_to_user((struct watchdog_info *)arg, &gemini_wdt_info,
  109. sizeof(gemini_wdt_info)) ? -EFAULT : 0;
  110. case WDIOC_SETTIMEOUT:
  111. if (get_user(value, (int *)arg))
  112. return -EFAULT;
  113. if ((value < 1) || (value > WDT_MAX_TIMEOUT))
  114. return -EINVAL;
  115. timeout = value;
  116. /* restart wdt to use new timeout */
  117. gemini_wdt_stop(gemini_wdt);
  118. gemini_wdt_start(gemini_wdt);
  119. /* Fall through */
  120. case WDIOC_GETTIMEOUT:
  121. return put_user(timeout, (int *)arg);
  122. case WDIOC_GETTIMELEFT:
  123. value = __raw_readl(gemini_wdt->base + GEMINI_WDCOUNTER);
  124. return put_user(value / WDT_CLOCK, (int *)arg);
  125. default:
  126. return -ENOTTY;
  127. }
  128. }
  129. /* Refresh the watchdog whenever device is written to. */
  130. static ssize_t gemini_wdt_write(struct file *file, const char *data,
  131. size_t len, loff_t *ppos)
  132. {
  133. struct gemini_wdt_struct *gemini_wdt = file->private_data;
  134. if (len) {
  135. if (!nowayout) {
  136. size_t i;
  137. clear_bit(WDT_OK_TO_CLOSE, &gemini_wdt->status);
  138. for (i = 0; i != len; i++) {
  139. char c;
  140. if (get_user(c, data + i))
  141. return -EFAULT;
  142. if (c == 'V')
  143. set_bit(WDT_OK_TO_CLOSE,
  144. &gemini_wdt->status);
  145. }
  146. }
  147. gemini_wdt_service(gemini_wdt);
  148. }
  149. return len;
  150. }
  151. static const struct file_operations gemini_wdt_fops = {
  152. .owner = THIS_MODULE,
  153. .llseek = no_llseek,
  154. .unlocked_ioctl = gemini_wdt_ioctl,
  155. .open = gemini_wdt_open,
  156. .release = gemini_wdt_close,
  157. .write = gemini_wdt_write,
  158. };
  159. static struct miscdevice gemini_wdt_miscdev = {
  160. .minor = WATCHDOG_MINOR,
  161. .name = "watchdog",
  162. .fops = &gemini_wdt_fops,
  163. };
  164. static void gemini_wdt_shutdown(struct platform_device *pdev)
  165. {
  166. struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
  167. gemini_wdt_stop(gemini_wdt);
  168. }
  169. static int gemini_wdt_probe(struct platform_device *pdev)
  170. {
  171. int ret;
  172. int res_size;
  173. struct resource *res;
  174. void __iomem *base;
  175. struct gemini_wdt_struct *gemini_wdt;
  176. unsigned int reg;
  177. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  178. if (!res) {
  179. dev_err(&pdev->dev, "can't get device resources\n");
  180. return -ENODEV;
  181. }
  182. res_size = resource_size(res);
  183. if (!request_mem_region(res->start, res_size, res->name)) {
  184. dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
  185. res_size, res->start);
  186. return -ENOMEM;
  187. }
  188. base = ioremap(res->start, res_size);
  189. if (!base) {
  190. dev_err(&pdev->dev, "ioremap failed\n");
  191. ret = -EIO;
  192. goto fail0;
  193. }
  194. gemini_wdt = kzalloc(sizeof(struct gemini_wdt_struct), GFP_KERNEL);
  195. if (!gemini_wdt) {
  196. dev_err(&pdev->dev, "can't allocate interface\n");
  197. ret = -ENOMEM;
  198. goto fail1;
  199. }
  200. /* Setup gemini_wdt driver structure */
  201. gemini_wdt->base = base;
  202. gemini_wdt->res = res;
  203. /* Set up platform driver data */
  204. platform_set_drvdata(pdev, gemini_wdt);
  205. gemini_wdt_dev = pdev;
  206. if (gemini_wdt_miscdev.parent) {
  207. ret = -EBUSY;
  208. goto fail2;
  209. }
  210. gemini_wdt_miscdev.parent = &pdev->dev;
  211. reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
  212. if (reg & WDCR_ENABLE) {
  213. /* Watchdog was enabled by the bootloader, disable it. */
  214. reg &= ~(WDCR_ENABLE);
  215. __raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
  216. }
  217. ret = misc_register(&gemini_wdt_miscdev);
  218. if (ret)
  219. goto fail2;
  220. return 0;
  221. fail2:
  222. platform_set_drvdata(pdev, NULL);
  223. kfree(gemini_wdt);
  224. fail1:
  225. iounmap(base);
  226. fail0:
  227. release_mem_region(res->start, res_size);
  228. return ret;
  229. }
  230. static int gemini_wdt_remove(struct platform_device *pdev)
  231. {
  232. struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
  233. platform_set_drvdata(pdev, NULL);
  234. misc_deregister(&gemini_wdt_miscdev);
  235. gemini_wdt_dev = NULL;
  236. iounmap(gemini_wdt->base);
  237. release_mem_region(gemini_wdt->res->start, resource_size(gemini_wdt->res));
  238. kfree(gemini_wdt);
  239. return 0;
  240. }
  241. #ifdef CONFIG_PM
  242. static int gemini_wdt_suspend(struct platform_device *pdev, pm_message_t message)
  243. {
  244. struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
  245. unsigned int reg;
  246. reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
  247. reg &= ~(WDCR_WDENABLE);
  248. __raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
  249. return 0;
  250. }
  251. static int gemini_wdt_resume(struct platform_device *pdev)
  252. {
  253. struct gemini_wdt_struct *gemini_wdt = platform_get_drvdata(pdev);
  254. unsigned int reg;
  255. if (gemini_wdt->status) {
  256. reg = __raw_readw(gemini_wdt->base + GEMINI_WDCR);
  257. reg |= WDCR_WDENABLE;
  258. __raw_writel(reg, gemini_wdt->base + GEMINI_WDCR);
  259. }
  260. return 0;
  261. }
  262. #else
  263. #define gemini_wdt_suspend NULL
  264. #define gemini_wdt_resume NULL
  265. #endif
  266. static struct platform_driver gemini_wdt_driver = {
  267. .probe = gemini_wdt_probe,
  268. .remove = gemini_wdt_remove,
  269. .shutdown = gemini_wdt_shutdown,
  270. .suspend = gemini_wdt_suspend,
  271. .resume = gemini_wdt_resume,
  272. .driver = {
  273. .name = "gemini-wdt",
  274. .owner = THIS_MODULE,
  275. },
  276. };
  277. static int __init gemini_wdt_init(void)
  278. {
  279. return platform_driver_probe(&gemini_wdt_driver, gemini_wdt_probe);
  280. }
  281. static void __exit gemini_wdt_exit(void)
  282. {
  283. platform_driver_unregister(&gemini_wdt_driver);
  284. }
  285. module_init(gemini_wdt_init);
  286. module_exit(gemini_wdt_exit);
  287. module_param(timeout, uint, 0);
  288. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
  289. module_param(nowayout, int, 0);
  290. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  291. MODULE_AUTHOR("Paulius Zaleckas");
  292. MODULE_DESCRIPTION("Watchdog driver for Gemini");
  293. MODULE_LICENSE("GPL");
  294. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  295. MODULE_ALIAS("platform:gemini-wdt");