at32ap700x_wdt.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. #include <linux/slab.h>
  35. #define TIMEOUT_MIN 1
  36. #define TIMEOUT_MAX 2
  37. #define TIMEOUT_DEFAULT TIMEOUT_MAX
  38. /* module parameters */
  39. static int timeout = TIMEOUT_DEFAULT;
  40. module_param(timeout, int, 0);
  41. MODULE_PARM_DESC(timeout,
  42. "Timeout value. Limited to be 1 or 2 seconds. (default="
  43. __MODULE_STRING(TIMEOUT_DEFAULT) ")");
  44. static bool nowayout = WATCHDOG_NOWAYOUT;
  45. module_param(nowayout, bool, 0);
  46. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  47. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  48. /* Watchdog registers and write/read macro */
  49. #define WDT_CTRL 0x00
  50. #define WDT_CTRL_EN 0
  51. #define WDT_CTRL_PSEL 8
  52. #define WDT_CTRL_KEY 24
  53. #define WDT_CLR 0x04
  54. #define WDT_RCAUSE 0x10
  55. #define WDT_RCAUSE_POR 0
  56. #define WDT_RCAUSE_EXT 2
  57. #define WDT_RCAUSE_WDT 3
  58. #define WDT_RCAUSE_JTAG 4
  59. #define WDT_RCAUSE_SERP 5
  60. #define WDT_BIT(name) (1 << WDT_##name)
  61. #define WDT_BF(name, value) ((value) << WDT_##name)
  62. #define wdt_readl(dev, reg) \
  63. __raw_readl((dev)->regs + WDT_##reg)
  64. #define wdt_writel(dev, reg, value) \
  65. __raw_writel((value), (dev)->regs + WDT_##reg)
  66. struct wdt_at32ap700x {
  67. void __iomem *regs;
  68. spinlock_t io_lock;
  69. int timeout;
  70. int boot_status;
  71. unsigned long users;
  72. struct miscdevice miscdev;
  73. };
  74. static struct wdt_at32ap700x *wdt;
  75. static char expect_release;
  76. /*
  77. * Disable the watchdog.
  78. */
  79. static inline void at32_wdt_stop(void)
  80. {
  81. unsigned long psel;
  82. spin_lock(&wdt->io_lock);
  83. psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
  84. wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
  85. wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
  86. spin_unlock(&wdt->io_lock);
  87. }
  88. /*
  89. * Enable and reset the watchdog.
  90. */
  91. static inline void at32_wdt_start(void)
  92. {
  93. /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
  94. unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
  95. spin_lock(&wdt->io_lock);
  96. wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
  97. | WDT_BF(CTRL_PSEL, psel)
  98. | WDT_BF(CTRL_KEY, 0x55));
  99. wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
  100. | WDT_BF(CTRL_PSEL, psel)
  101. | WDT_BF(CTRL_KEY, 0xaa));
  102. spin_unlock(&wdt->io_lock);
  103. }
  104. /*
  105. * Pat the watchdog timer.
  106. */
  107. static inline void at32_wdt_pat(void)
  108. {
  109. spin_lock(&wdt->io_lock);
  110. wdt_writel(wdt, CLR, 0x42);
  111. spin_unlock(&wdt->io_lock);
  112. }
  113. /*
  114. * Watchdog device is opened, and watchdog starts running.
  115. */
  116. static int at32_wdt_open(struct inode *inode, struct file *file)
  117. {
  118. if (test_and_set_bit(1, &wdt->users))
  119. return -EBUSY;
  120. at32_wdt_start();
  121. return nonseekable_open(inode, file);
  122. }
  123. /*
  124. * Close the watchdog device.
  125. */
  126. static int at32_wdt_close(struct inode *inode, struct file *file)
  127. {
  128. if (expect_release == 42) {
  129. at32_wdt_stop();
  130. } else {
  131. dev_dbg(wdt->miscdev.parent,
  132. "unexpected close, not stopping watchdog!\n");
  133. at32_wdt_pat();
  134. }
  135. clear_bit(1, &wdt->users);
  136. expect_release = 0;
  137. return 0;
  138. }
  139. /*
  140. * Change the watchdog time interval.
  141. */
  142. static int at32_wdt_settimeout(int time)
  143. {
  144. /*
  145. * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
  146. * 2 ^ 16 allowing up to 2 seconds timeout.
  147. */
  148. if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
  149. return -EINVAL;
  150. /*
  151. * Set new watchdog time. It will be used when at32_wdt_start() is
  152. * called.
  153. */
  154. wdt->timeout = time;
  155. return 0;
  156. }
  157. /*
  158. * Get the watchdog status.
  159. */
  160. static int at32_wdt_get_status(void)
  161. {
  162. int rcause;
  163. int status = 0;
  164. rcause = wdt_readl(wdt, RCAUSE);
  165. switch (rcause) {
  166. case WDT_BIT(RCAUSE_EXT):
  167. status = WDIOF_EXTERN1;
  168. break;
  169. case WDT_BIT(RCAUSE_WDT):
  170. status = WDIOF_CARDRESET;
  171. break;
  172. case WDT_BIT(RCAUSE_POR): /* fall through */
  173. case WDT_BIT(RCAUSE_JTAG): /* fall through */
  174. case WDT_BIT(RCAUSE_SERP): /* fall through */
  175. default:
  176. break;
  177. }
  178. return status;
  179. }
  180. static const struct watchdog_info at32_wdt_info = {
  181. .identity = "at32ap700x watchdog",
  182. .options = WDIOF_SETTIMEOUT |
  183. WDIOF_KEEPALIVEPING |
  184. WDIOF_MAGICCLOSE,
  185. };
  186. /*
  187. * Handle commands from user-space.
  188. */
  189. static long at32_wdt_ioctl(struct file *file,
  190. unsigned int cmd, unsigned long arg)
  191. {
  192. int ret = -ENOTTY;
  193. int time;
  194. void __user *argp = (void __user *)arg;
  195. int __user *p = argp;
  196. switch (cmd) {
  197. case WDIOC_GETSUPPORT:
  198. ret = copy_to_user(argp, &at32_wdt_info,
  199. sizeof(at32_wdt_info)) ? -EFAULT : 0;
  200. break;
  201. case WDIOC_GETSTATUS:
  202. ret = put_user(0, p);
  203. break;
  204. case WDIOC_GETBOOTSTATUS:
  205. ret = put_user(wdt->boot_status, p);
  206. break;
  207. case WDIOC_SETOPTIONS:
  208. ret = get_user(time, p);
  209. if (ret)
  210. break;
  211. if (time & WDIOS_DISABLECARD)
  212. at32_wdt_stop();
  213. if (time & WDIOS_ENABLECARD)
  214. at32_wdt_start();
  215. ret = 0;
  216. break;
  217. case WDIOC_KEEPALIVE:
  218. at32_wdt_pat();
  219. ret = 0;
  220. break;
  221. case WDIOC_SETTIMEOUT:
  222. ret = get_user(time, p);
  223. if (ret)
  224. break;
  225. ret = at32_wdt_settimeout(time);
  226. if (ret)
  227. break;
  228. /* Enable new time value */
  229. at32_wdt_start();
  230. /* fall through */
  231. case WDIOC_GETTIMEOUT:
  232. ret = put_user(wdt->timeout, p);
  233. break;
  234. }
  235. return ret;
  236. }
  237. static ssize_t at32_wdt_write(struct file *file, const char __user *data,
  238. size_t len, loff_t *ppos)
  239. {
  240. /* See if we got the magic character 'V' and reload the timer */
  241. if (len) {
  242. if (!nowayout) {
  243. size_t i;
  244. /*
  245. * note: just in case someone wrote the magic
  246. * character five months ago...
  247. */
  248. expect_release = 0;
  249. /*
  250. * scan to see whether or not we got the magic
  251. * character
  252. */
  253. for (i = 0; i != len; i++) {
  254. char c;
  255. if (get_user(c, data + i))
  256. return -EFAULT;
  257. if (c == 'V')
  258. expect_release = 42;
  259. }
  260. }
  261. /* someone wrote to us, we should pat the watchdog */
  262. at32_wdt_pat();
  263. }
  264. return len;
  265. }
  266. static const struct file_operations at32_wdt_fops = {
  267. .owner = THIS_MODULE,
  268. .llseek = no_llseek,
  269. .unlocked_ioctl = at32_wdt_ioctl,
  270. .open = at32_wdt_open,
  271. .release = at32_wdt_close,
  272. .write = at32_wdt_write,
  273. };
  274. static int __init at32_wdt_probe(struct platform_device *pdev)
  275. {
  276. struct resource *regs;
  277. int ret;
  278. if (wdt) {
  279. dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
  280. return -EBUSY;
  281. }
  282. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  283. if (!regs) {
  284. dev_dbg(&pdev->dev, "missing mmio resource\n");
  285. return -ENXIO;
  286. }
  287. wdt = devm_kzalloc(&pdev->dev, sizeof(struct wdt_at32ap700x),
  288. GFP_KERNEL);
  289. if (!wdt)
  290. return -ENOMEM;
  291. wdt->regs = devm_ioremap(&pdev->dev, 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_free;
  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. wdt->miscdev.parent = &pdev->dev;
  312. platform_set_drvdata(pdev, wdt);
  313. if (at32_wdt_settimeout(timeout)) {
  314. at32_wdt_settimeout(TIMEOUT_DEFAULT);
  315. dev_dbg(&pdev->dev,
  316. "default timeout invalid, set to %d sec.\n",
  317. TIMEOUT_DEFAULT);
  318. }
  319. ret = misc_register(&wdt->miscdev);
  320. if (ret) {
  321. dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
  322. goto err_free;
  323. }
  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_free:
  329. wdt = NULL;
  330. return ret;
  331. }
  332. static int __exit at32_wdt_remove(struct platform_device *pdev)
  333. {
  334. if (wdt && platform_get_drvdata(pdev) == wdt) {
  335. /* Stop the timer before we leave */
  336. if (!nowayout)
  337. at32_wdt_stop();
  338. misc_deregister(&wdt->miscdev);
  339. wdt = NULL;
  340. }
  341. return 0;
  342. }
  343. static void at32_wdt_shutdown(struct platform_device *pdev)
  344. {
  345. at32_wdt_stop();
  346. }
  347. #ifdef CONFIG_PM
  348. static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
  349. {
  350. at32_wdt_stop();
  351. return 0;
  352. }
  353. static int at32_wdt_resume(struct platform_device *pdev)
  354. {
  355. if (wdt->users)
  356. at32_wdt_start();
  357. return 0;
  358. }
  359. #else
  360. #define at32_wdt_suspend NULL
  361. #define at32_wdt_resume NULL
  362. #endif
  363. /* work with hotplug and coldplug */
  364. MODULE_ALIAS("platform:at32_wdt");
  365. static struct platform_driver at32_wdt_driver = {
  366. .remove = __exit_p(at32_wdt_remove),
  367. .suspend = at32_wdt_suspend,
  368. .resume = at32_wdt_resume,
  369. .driver = {
  370. .name = "at32_wdt",
  371. },
  372. .shutdown = at32_wdt_shutdown,
  373. };
  374. module_platform_driver_probe(at32_wdt_driver, at32_wdt_probe);
  375. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
  376. MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
  377. MODULE_LICENSE("GPL");