s3c2410_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* linux/drivers/char/watchdog/s3c2410_wdt.c
  2. *
  3. * Copyright (c) 2004 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 Watchdog Timer Support
  7. *
  8. * Based on, softdog.c by Alan Cox,
  9. * (c) Copyright 1996 Alan Cox <alan@redhat.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Changelog:
  26. * 05-Oct-2004 BJD Added semaphore init to stop crashes on open
  27. * Fixed tmr_count / wdt_count confusion
  28. * Added configurable debug
  29. *
  30. * 11-Jan-2005 BJD Fixed divide-by-2 in timeout code
  31. *
  32. * 25-Jan-2005 DA Added suspend/resume support
  33. * Replaced reboot notifier with .shutdown method
  34. *
  35. * 10-Mar-2005 LCVR Changed S3C2410_VA to S3C24XX_VA
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/types.h>
  40. #include <linux/timer.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/watchdog.h>
  43. #include <linux/fs.h>
  44. #include <linux/init.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/interrupt.h>
  47. #include <linux/clk.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/io.h>
  50. #include <asm/arch/map.h>
  51. #undef S3C24XX_VA_WATCHDOG
  52. #define S3C24XX_VA_WATCHDOG (0)
  53. #include <asm/arch/regs-watchdog.h>
  54. #define PFX "s3c2410-wdt: "
  55. #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
  56. #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
  57. static int nowayout = WATCHDOG_NOWAYOUT;
  58. static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
  59. static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
  60. static int soft_noboot = 0;
  61. static int debug = 0;
  62. module_param(tmr_margin, int, 0);
  63. module_param(tmr_atboot, int, 0);
  64. module_param(nowayout, int, 0);
  65. module_param(soft_noboot, int, 0);
  66. module_param(debug, int, 0);
  67. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  68. MODULE_PARM_DESC(tmr_atboot, "Watchdog is started at boot time if set to 1, default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
  69. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  70. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
  71. MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
  72. typedef enum close_state {
  73. CLOSE_STATE_NOT,
  74. CLOSE_STATE_ALLOW=0x4021
  75. } close_state_t;
  76. static DECLARE_MUTEX(open_lock);
  77. static struct resource *wdt_mem;
  78. static struct resource *wdt_irq;
  79. static struct clk *wdt_clock;
  80. static void __iomem *wdt_base;
  81. static unsigned int wdt_count;
  82. static close_state_t allow_close;
  83. /* watchdog control routines */
  84. #define DBG(msg...) do { \
  85. if (debug) \
  86. printk(KERN_INFO msg); \
  87. } while(0)
  88. /* functions */
  89. static int s3c2410wdt_keepalive(void)
  90. {
  91. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  92. return 0;
  93. }
  94. static int s3c2410wdt_stop(void)
  95. {
  96. unsigned long wtcon;
  97. wtcon = readl(wdt_base + S3C2410_WTCON);
  98. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  99. writel(wtcon, wdt_base + S3C2410_WTCON);
  100. return 0;
  101. }
  102. static int s3c2410wdt_start(void)
  103. {
  104. unsigned long wtcon;
  105. s3c2410wdt_stop();
  106. wtcon = readl(wdt_base + S3C2410_WTCON);
  107. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  108. if (soft_noboot) {
  109. wtcon |= S3C2410_WTCON_INTEN;
  110. wtcon &= ~S3C2410_WTCON_RSTEN;
  111. } else {
  112. wtcon &= ~S3C2410_WTCON_INTEN;
  113. wtcon |= S3C2410_WTCON_RSTEN;
  114. }
  115. DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
  116. __FUNCTION__, wdt_count, wtcon);
  117. writel(wdt_count, wdt_base + S3C2410_WTDAT);
  118. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  119. writel(wtcon, wdt_base + S3C2410_WTCON);
  120. return 0;
  121. }
  122. static int s3c2410wdt_set_heartbeat(int timeout)
  123. {
  124. unsigned int freq = clk_get_rate(wdt_clock);
  125. unsigned int count;
  126. unsigned int divisor = 1;
  127. unsigned long wtcon;
  128. if (timeout < 1)
  129. return -EINVAL;
  130. freq /= 128;
  131. count = timeout * freq;
  132. DBG("%s: count=%d, timeout=%d, freq=%d\n",
  133. __FUNCTION__, count, timeout, freq);
  134. /* if the count is bigger than the watchdog register,
  135. then work out what we need to do (and if) we can
  136. actually make this value
  137. */
  138. if (count >= 0x10000) {
  139. for (divisor = 1; divisor <= 0x100; divisor++) {
  140. if ((count / divisor) < 0x10000)
  141. break;
  142. }
  143. if ((count / divisor) >= 0x10000) {
  144. printk(KERN_ERR PFX "timeout %d too big\n", timeout);
  145. return -EINVAL;
  146. }
  147. }
  148. tmr_margin = timeout;
  149. DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
  150. __FUNCTION__, timeout, divisor, count, count/divisor);
  151. count /= divisor;
  152. wdt_count = count;
  153. /* update the pre-scaler */
  154. wtcon = readl(wdt_base + S3C2410_WTCON);
  155. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  156. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  157. writel(count, wdt_base + S3C2410_WTDAT);
  158. writel(wtcon, wdt_base + S3C2410_WTCON);
  159. return 0;
  160. }
  161. /*
  162. * /dev/watchdog handling
  163. */
  164. static int s3c2410wdt_open(struct inode *inode, struct file *file)
  165. {
  166. if(down_trylock(&open_lock))
  167. return -EBUSY;
  168. if (nowayout) {
  169. __module_get(THIS_MODULE);
  170. } else {
  171. allow_close = CLOSE_STATE_ALLOW;
  172. }
  173. /* start the timer */
  174. s3c2410wdt_start();
  175. return nonseekable_open(inode, file);
  176. }
  177. static int s3c2410wdt_release(struct inode *inode, struct file *file)
  178. {
  179. /*
  180. * Shut off the timer.
  181. * Lock it in if it's a module and we set nowayout
  182. */
  183. if (allow_close == CLOSE_STATE_ALLOW) {
  184. s3c2410wdt_stop();
  185. } else {
  186. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  187. s3c2410wdt_keepalive();
  188. }
  189. allow_close = CLOSE_STATE_NOT;
  190. up(&open_lock);
  191. return 0;
  192. }
  193. static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
  194. size_t len, loff_t *ppos)
  195. {
  196. /*
  197. * Refresh the timer.
  198. */
  199. if(len) {
  200. if (!nowayout) {
  201. size_t i;
  202. /* In case it was set long ago */
  203. allow_close = CLOSE_STATE_NOT;
  204. for (i = 0; i != len; i++) {
  205. char c;
  206. if (get_user(c, data + i))
  207. return -EFAULT;
  208. if (c == 'V')
  209. allow_close = CLOSE_STATE_ALLOW;
  210. }
  211. }
  212. s3c2410wdt_keepalive();
  213. }
  214. return len;
  215. }
  216. #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
  217. static struct watchdog_info s3c2410_wdt_ident = {
  218. .options = OPTIONS,
  219. .firmware_version = 0,
  220. .identity = "S3C2410 Watchdog",
  221. };
  222. static int s3c2410wdt_ioctl(struct inode *inode, struct file *file,
  223. unsigned int cmd, unsigned long arg)
  224. {
  225. void __user *argp = (void __user *)arg;
  226. int __user *p = argp;
  227. int new_margin;
  228. switch (cmd) {
  229. default:
  230. return -ENOIOCTLCMD;
  231. case WDIOC_GETSUPPORT:
  232. return copy_to_user(argp, &s3c2410_wdt_ident,
  233. sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
  234. case WDIOC_GETSTATUS:
  235. case WDIOC_GETBOOTSTATUS:
  236. return put_user(0, p);
  237. case WDIOC_KEEPALIVE:
  238. s3c2410wdt_keepalive();
  239. return 0;
  240. case WDIOC_SETTIMEOUT:
  241. if (get_user(new_margin, p))
  242. return -EFAULT;
  243. if (s3c2410wdt_set_heartbeat(new_margin))
  244. return -EINVAL;
  245. s3c2410wdt_keepalive();
  246. return put_user(tmr_margin, p);
  247. case WDIOC_GETTIMEOUT:
  248. return put_user(tmr_margin, p);
  249. }
  250. }
  251. /* kernel interface */
  252. static const struct file_operations s3c2410wdt_fops = {
  253. .owner = THIS_MODULE,
  254. .llseek = no_llseek,
  255. .write = s3c2410wdt_write,
  256. .ioctl = s3c2410wdt_ioctl,
  257. .open = s3c2410wdt_open,
  258. .release = s3c2410wdt_release,
  259. };
  260. static struct miscdevice s3c2410wdt_miscdev = {
  261. .minor = WATCHDOG_MINOR,
  262. .name = "watchdog",
  263. .fops = &s3c2410wdt_fops,
  264. };
  265. /* interrupt handler code */
  266. static irqreturn_t s3c2410wdt_irq(int irqno, void *param,
  267. struct pt_regs *regs)
  268. {
  269. printk(KERN_INFO PFX "Watchdog timer expired!\n");
  270. s3c2410wdt_keepalive();
  271. return IRQ_HANDLED;
  272. }
  273. /* device interface */
  274. static int s3c2410wdt_probe(struct platform_device *pdev)
  275. {
  276. struct resource *res;
  277. int started = 0;
  278. int ret;
  279. int size;
  280. DBG("%s: probe=%p\n", __FUNCTION__, pdev);
  281. /* get the memory region for the watchdog timer */
  282. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  283. if (res == NULL) {
  284. printk(KERN_INFO PFX "failed to get memory region resouce\n");
  285. return -ENOENT;
  286. }
  287. size = (res->end-res->start)+1;
  288. wdt_mem = request_mem_region(res->start, size, pdev->name);
  289. if (wdt_mem == NULL) {
  290. printk(KERN_INFO PFX "failed to get memory region\n");
  291. return -ENOENT;
  292. }
  293. wdt_base = ioremap(res->start, size);
  294. if (wdt_base == 0) {
  295. printk(KERN_INFO PFX "failed to ioremap() region\n");
  296. return -EINVAL;
  297. }
  298. DBG("probe: mapped wdt_base=%p\n", wdt_base);
  299. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  300. if (res == NULL) {
  301. printk(KERN_INFO PFX "failed to get irq resource\n");
  302. return -ENOENT;
  303. }
  304. ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, pdev);
  305. if (ret != 0) {
  306. printk(KERN_INFO PFX "failed to install irq (%d)\n", ret);
  307. return ret;
  308. }
  309. wdt_clock = clk_get(&pdev->dev, "watchdog");
  310. if (wdt_clock == NULL) {
  311. printk(KERN_INFO PFX "failed to find watchdog clock source\n");
  312. return -ENOENT;
  313. }
  314. clk_enable(wdt_clock);
  315. /* see if we can actually set the requested timer margin, and if
  316. * not, try the default value */
  317. if (s3c2410wdt_set_heartbeat(tmr_margin)) {
  318. started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  319. if (started == 0) {
  320. printk(KERN_INFO PFX "tmr_margin value out of range, default %d used\n",
  321. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  322. } else {
  323. printk(KERN_INFO PFX "default timer value is out of range, cannot start\n");
  324. }
  325. }
  326. ret = misc_register(&s3c2410wdt_miscdev);
  327. if (ret) {
  328. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (%d)\n",
  329. WATCHDOG_MINOR, ret);
  330. return ret;
  331. }
  332. if (tmr_atboot && started == 0) {
  333. printk(KERN_INFO PFX "Starting Watchdog Timer\n");
  334. s3c2410wdt_start();
  335. } else if (!tmr_atboot) {
  336. /* if we're not enabling the watchdog, then ensure it is
  337. * disabled if it has been left running from the bootloader
  338. * or other source */
  339. s3c2410wdt_stop();
  340. }
  341. return 0;
  342. }
  343. static int s3c2410wdt_remove(struct platform_device *dev)
  344. {
  345. if (wdt_mem != NULL) {
  346. release_resource(wdt_mem);
  347. kfree(wdt_mem);
  348. wdt_mem = NULL;
  349. }
  350. if (wdt_irq != NULL) {
  351. free_irq(wdt_irq->start, dev);
  352. wdt_irq = NULL;
  353. }
  354. if (wdt_clock != NULL) {
  355. clk_disable(wdt_clock);
  356. clk_put(wdt_clock);
  357. wdt_clock = NULL;
  358. }
  359. misc_deregister(&s3c2410wdt_miscdev);
  360. return 0;
  361. }
  362. static void s3c2410wdt_shutdown(struct platform_device *dev)
  363. {
  364. s3c2410wdt_stop();
  365. }
  366. #ifdef CONFIG_PM
  367. static unsigned long wtcon_save;
  368. static unsigned long wtdat_save;
  369. static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
  370. {
  371. /* Save watchdog state, and turn it off. */
  372. wtcon_save = readl(wdt_base + S3C2410_WTCON);
  373. wtdat_save = readl(wdt_base + S3C2410_WTDAT);
  374. /* Note that WTCNT doesn't need to be saved. */
  375. s3c2410wdt_stop();
  376. return 0;
  377. }
  378. static int s3c2410wdt_resume(struct platform_device *dev)
  379. {
  380. /* Restore watchdog state. */
  381. writel(wtdat_save, wdt_base + S3C2410_WTDAT);
  382. writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
  383. writel(wtcon_save, wdt_base + S3C2410_WTCON);
  384. printk(KERN_INFO PFX "watchdog %sabled\n",
  385. (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
  386. return 0;
  387. }
  388. #else
  389. #define s3c2410wdt_suspend NULL
  390. #define s3c2410wdt_resume NULL
  391. #endif /* CONFIG_PM */
  392. static struct platform_driver s3c2410wdt_driver = {
  393. .probe = s3c2410wdt_probe,
  394. .remove = s3c2410wdt_remove,
  395. .shutdown = s3c2410wdt_shutdown,
  396. .suspend = s3c2410wdt_suspend,
  397. .resume = s3c2410wdt_resume,
  398. .driver = {
  399. .owner = THIS_MODULE,
  400. .name = "s3c2410-wdt",
  401. },
  402. };
  403. static char banner[] __initdata = KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
  404. static int __init watchdog_init(void)
  405. {
  406. printk(banner);
  407. return platform_driver_register(&s3c2410wdt_driver);
  408. }
  409. static void __exit watchdog_exit(void)
  410. {
  411. platform_driver_unregister(&s3c2410wdt_driver);
  412. }
  413. module_init(watchdog_init);
  414. module_exit(watchdog_exit);
  415. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
  416. "Dimitry Andric <dimitry.andric@tomtom.com>");
  417. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);