at32ap700x_wdt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Watchdog driver for Atmel AT32AP700X devices
  3. *
  4. * Copyright (C) 2005-2006 Atmel Corporation
  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/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/fs.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/watchdog.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/io.h>
  20. #include <linux/spinlock.h>
  21. #define TIMEOUT_MIN 1
  22. #define TIMEOUT_MAX 2
  23. #define TIMEOUT_DEFAULT TIMEOUT_MAX
  24. /* module parameters */
  25. static int timeout = TIMEOUT_DEFAULT;
  26. module_param(timeout, int, 0);
  27. MODULE_PARM_DESC(timeout,
  28. "Timeout value. Limited to be 1 or 2 seconds. (default="
  29. __MODULE_STRING(TIMEOUT_DEFAULT) ")");
  30. static int nowayout = WATCHDOG_NOWAYOUT;
  31. module_param(nowayout, int, 0);
  32. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  33. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  34. /* Watchdog registers and write/read macro */
  35. #define WDT_CTRL 0x00
  36. #define WDT_CTRL_EN 0
  37. #define WDT_CTRL_PSEL 8
  38. #define WDT_CTRL_KEY 24
  39. #define WDT_CLR 0x04
  40. #define WDT_BIT(name) (1 << WDT_##name)
  41. #define WDT_BF(name, value) ((value) << WDT_##name)
  42. #define wdt_readl(dev, reg) \
  43. __raw_readl((dev)->regs + WDT_##reg)
  44. #define wdt_writel(dev, reg, value) \
  45. __raw_writel((value), (dev)->regs + WDT_##reg)
  46. struct wdt_at32ap700x {
  47. void __iomem *regs;
  48. spinlock_t io_lock;
  49. int timeout;
  50. unsigned long users;
  51. struct miscdevice miscdev;
  52. };
  53. static struct wdt_at32ap700x *wdt;
  54. static char expect_release;
  55. /*
  56. * Disable the watchdog.
  57. */
  58. static inline void at32_wdt_stop(void)
  59. {
  60. unsigned long psel;
  61. spin_lock(&wdt->io_lock);
  62. psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
  63. wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
  64. wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
  65. spin_unlock(&wdt->io_lock);
  66. }
  67. /*
  68. * Enable and reset the watchdog.
  69. */
  70. static inline void at32_wdt_start(void)
  71. {
  72. /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
  73. unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
  74. spin_lock(&wdt->io_lock);
  75. wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
  76. | WDT_BF(CTRL_PSEL, psel)
  77. | WDT_BF(CTRL_KEY, 0x55));
  78. wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
  79. | WDT_BF(CTRL_PSEL, psel)
  80. | WDT_BF(CTRL_KEY, 0xaa));
  81. spin_unlock(&wdt->io_lock);
  82. }
  83. /*
  84. * Pat the watchdog timer.
  85. */
  86. static inline void at32_wdt_pat(void)
  87. {
  88. spin_lock(&wdt->io_lock);
  89. wdt_writel(wdt, CLR, 0x42);
  90. spin_unlock(&wdt->io_lock);
  91. }
  92. /*
  93. * Watchdog device is opened, and watchdog starts running.
  94. */
  95. static int at32_wdt_open(struct inode *inode, struct file *file)
  96. {
  97. if (test_and_set_bit(1, &wdt->users))
  98. return -EBUSY;
  99. at32_wdt_start();
  100. return nonseekable_open(inode, file);
  101. }
  102. /*
  103. * Close the watchdog device.
  104. */
  105. static int at32_wdt_close(struct inode *inode, struct file *file)
  106. {
  107. if (expect_release == 42) {
  108. at32_wdt_stop();
  109. } else {
  110. dev_dbg(wdt->miscdev.parent,
  111. "Unexpected close, not stopping watchdog!\n");
  112. at32_wdt_pat();
  113. }
  114. clear_bit(1, &wdt->users);
  115. expect_release = 0;
  116. return 0;
  117. }
  118. /*
  119. * Change the watchdog time interval.
  120. */
  121. static int at32_wdt_settimeout(int time)
  122. {
  123. /*
  124. * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
  125. * 2 ^ 16 allowing up to 2 seconds timeout.
  126. */
  127. if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
  128. return -EINVAL;
  129. /*
  130. * Set new watchdog time. It will be used when at32_wdt_start() is
  131. * called.
  132. */
  133. wdt->timeout = time;
  134. return 0;
  135. }
  136. static struct watchdog_info at32_wdt_info = {
  137. .identity = "at32ap700x watchdog",
  138. .options = WDIOF_SETTIMEOUT |
  139. WDIOF_KEEPALIVEPING |
  140. WDIOF_MAGICCLOSE,
  141. };
  142. /*
  143. * Handle commands from user-space.
  144. */
  145. static int at32_wdt_ioctl(struct inode *inode, struct file *file,
  146. unsigned int cmd, unsigned long arg)
  147. {
  148. int ret = -ENOTTY;
  149. int time;
  150. void __user *argp = (void __user *)arg;
  151. int __user *p = argp;
  152. switch (cmd) {
  153. case WDIOC_KEEPALIVE:
  154. at32_wdt_pat();
  155. ret = 0;
  156. break;
  157. case WDIOC_GETSUPPORT:
  158. ret = copy_to_user(argp, &at32_wdt_info,
  159. sizeof(at32_wdt_info)) ? -EFAULT : 0;
  160. break;
  161. case WDIOC_SETTIMEOUT:
  162. ret = get_user(time, p);
  163. if (ret)
  164. break;
  165. ret = at32_wdt_settimeout(time);
  166. if (ret)
  167. break;
  168. /* Enable new time value */
  169. at32_wdt_start();
  170. /* fall through */
  171. case WDIOC_GETTIMEOUT:
  172. ret = put_user(wdt->timeout, p);
  173. break;
  174. case WDIOC_GETSTATUS: /* fall through */
  175. case WDIOC_GETBOOTSTATUS:
  176. ret = put_user(0, p);
  177. break;
  178. case WDIOC_SETOPTIONS:
  179. ret = get_user(time, p);
  180. if (ret)
  181. break;
  182. if (time & WDIOS_DISABLECARD)
  183. at32_wdt_stop();
  184. if (time & WDIOS_ENABLECARD)
  185. at32_wdt_start();
  186. ret = 0;
  187. break;
  188. }
  189. return ret;
  190. }
  191. static ssize_t at32_wdt_write(struct file *file, const char __user *data,
  192. size_t len, loff_t *ppos)
  193. {
  194. /* See if we got the magic character 'V' and reload the timer */
  195. if (len) {
  196. if (!nowayout) {
  197. size_t i;
  198. /*
  199. * note: just in case someone wrote the magic
  200. * character five months ago...
  201. */
  202. expect_release = 0;
  203. /*
  204. * scan to see whether or not we got the magic
  205. * character
  206. */
  207. for (i = 0; i != len; i++) {
  208. char c;
  209. if (get_user(c, data+i))
  210. return -EFAULT;
  211. if (c == 'V')
  212. expect_release = 42;
  213. }
  214. }
  215. /* someone wrote to us, we should pat the watchdog */
  216. at32_wdt_pat();
  217. }
  218. return len;
  219. }
  220. static const struct file_operations at32_wdt_fops = {
  221. .owner = THIS_MODULE,
  222. .llseek = no_llseek,
  223. .ioctl = at32_wdt_ioctl,
  224. .open = at32_wdt_open,
  225. .release = at32_wdt_close,
  226. .write = at32_wdt_write,
  227. };
  228. static int __init at32_wdt_probe(struct platform_device *pdev)
  229. {
  230. struct resource *regs;
  231. int ret;
  232. if (wdt) {
  233. dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
  234. return -EBUSY;
  235. }
  236. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. if (!regs) {
  238. dev_dbg(&pdev->dev, "missing mmio resource\n");
  239. return -ENXIO;
  240. }
  241. wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
  242. if (!wdt) {
  243. dev_dbg(&pdev->dev, "no memory for wdt structure\n");
  244. return -ENOMEM;
  245. }
  246. wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
  247. if (!wdt->regs) {
  248. ret = -ENOMEM;
  249. dev_dbg(&pdev->dev, "could not map I/O memory\n");
  250. goto err_free;
  251. }
  252. spin_lock_init(&wdt->io_lock);
  253. wdt->users = 0;
  254. wdt->miscdev.minor = WATCHDOG_MINOR;
  255. wdt->miscdev.name = "watchdog";
  256. wdt->miscdev.fops = &at32_wdt_fops;
  257. if (at32_wdt_settimeout(timeout)) {
  258. at32_wdt_settimeout(TIMEOUT_DEFAULT);
  259. dev_dbg(&pdev->dev,
  260. "default timeout invalid, set to %d sec.\n",
  261. TIMEOUT_DEFAULT);
  262. }
  263. ret = misc_register(&wdt->miscdev);
  264. if (ret) {
  265. dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
  266. goto err_iounmap;
  267. }
  268. platform_set_drvdata(pdev, wdt);
  269. wdt->miscdev.parent = &pdev->dev;
  270. dev_info(&pdev->dev,
  271. "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
  272. wdt->regs, wdt->timeout, nowayout);
  273. return 0;
  274. err_iounmap:
  275. iounmap(wdt->regs);
  276. err_free:
  277. kfree(wdt);
  278. wdt = NULL;
  279. return ret;
  280. }
  281. static int __exit at32_wdt_remove(struct platform_device *pdev)
  282. {
  283. if (wdt && platform_get_drvdata(pdev) == wdt) {
  284. /* Stop the timer before we leave */
  285. if (!nowayout)
  286. at32_wdt_stop();
  287. misc_deregister(&wdt->miscdev);
  288. iounmap(wdt->regs);
  289. kfree(wdt);
  290. wdt = NULL;
  291. platform_set_drvdata(pdev, NULL);
  292. }
  293. return 0;
  294. }
  295. static void at32_wdt_shutdown(struct platform_device *pdev)
  296. {
  297. at32_wdt_stop();
  298. }
  299. #ifdef CONFIG_PM
  300. static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
  301. {
  302. at32_wdt_stop();
  303. return 0;
  304. }
  305. static int at32_wdt_resume(struct platform_device *pdev)
  306. {
  307. if (wdt->users)
  308. at32_wdt_start();
  309. return 0;
  310. }
  311. #else
  312. #define at32_wdt_suspend NULL
  313. #define at32_wdt_resume NULL
  314. #endif
  315. static struct platform_driver at32_wdt_driver = {
  316. .remove = __exit_p(at32_wdt_remove),
  317. .suspend = at32_wdt_suspend,
  318. .resume = at32_wdt_resume,
  319. .driver = {
  320. .name = "at32_wdt",
  321. .owner = THIS_MODULE,
  322. },
  323. .shutdown = at32_wdt_shutdown,
  324. };
  325. static int __init at32_wdt_init(void)
  326. {
  327. return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
  328. }
  329. module_init(at32_wdt_init);
  330. static void __exit at32_wdt_exit(void)
  331. {
  332. platform_driver_unregister(&at32_wdt_driver);
  333. }
  334. module_exit(at32_wdt_exit);
  335. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  336. MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
  337. MODULE_LICENSE("GPL");
  338. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);