at32ap700x_wdt.c 10.0 KB

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