s3c2410_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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-2004 BJD Fixed divide-by-2 in timeout code
  31. *
  32. * 10-Mar-2005 LCVR Changed S3C2410_VA to S3C24XX_VA
  33. */
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/config.h>
  37. #include <linux/types.h>
  38. #include <linux/timer.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/watchdog.h>
  41. #include <linux/fs.h>
  42. #include <linux/notifier.h>
  43. #include <linux/reboot.h>
  44. #include <linux/init.h>
  45. #include <linux/device.h>
  46. #include <linux/interrupt.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/io.h>
  49. #include <asm/arch/map.h>
  50. #include <asm/hardware/clock.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. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  58. static int nowayout = 1;
  59. #else
  60. static int nowayout = 0;
  61. #endif
  62. static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
  63. static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
  64. static int soft_noboot = 0;
  65. static int debug = 0;
  66. module_param(tmr_margin, int, 0);
  67. module_param(tmr_atboot, int, 0);
  68. module_param(nowayout, int, 0);
  69. module_param(soft_noboot, int, 0);
  70. module_param(debug, int, 0);
  71. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  72. MODULE_PARM_DESC(tmr_atboot, "Watchdog is started at boot time if set to 1, default=" __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
  73. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  74. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default depends on ONLY_TESTING)");
  75. MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");
  76. typedef enum close_state {
  77. CLOSE_STATE_NOT,
  78. CLOSE_STATE_ALLOW=0x4021
  79. } close_state_t;
  80. static DECLARE_MUTEX(open_lock);
  81. static struct resource *wdt_mem;
  82. static struct resource *wdt_irq;
  83. static struct clk *wdt_clock;
  84. static void __iomem *wdt_base;
  85. static unsigned int wdt_count;
  86. static close_state_t allow_close;
  87. /* watchdog control routines */
  88. #define DBG(msg...) do { \
  89. if (debug) \
  90. printk(KERN_INFO msg); \
  91. } while(0)
  92. /* functions */
  93. static int s3c2410wdt_keepalive(void)
  94. {
  95. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  96. return 0;
  97. }
  98. static int s3c2410wdt_stop(void)
  99. {
  100. unsigned long wtcon;
  101. wtcon = readl(wdt_base + S3C2410_WTCON);
  102. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  103. writel(wtcon, wdt_base + S3C2410_WTCON);
  104. return 0;
  105. }
  106. static int s3c2410wdt_start(void)
  107. {
  108. unsigned long wtcon;
  109. s3c2410wdt_stop();
  110. wtcon = readl(wdt_base + S3C2410_WTCON);
  111. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  112. if (soft_noboot) {
  113. wtcon |= S3C2410_WTCON_INTEN;
  114. wtcon &= ~S3C2410_WTCON_RSTEN;
  115. } else {
  116. wtcon &= ~S3C2410_WTCON_INTEN;
  117. wtcon |= S3C2410_WTCON_RSTEN;
  118. }
  119. DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
  120. __FUNCTION__, wdt_count, wtcon);
  121. writel(wdt_count, wdt_base + S3C2410_WTDAT);
  122. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  123. writel(wtcon, wdt_base + S3C2410_WTCON);
  124. return 0;
  125. }
  126. static int s3c2410wdt_set_heartbeat(int timeout)
  127. {
  128. unsigned int freq = clk_get_rate(wdt_clock);
  129. unsigned int count;
  130. unsigned int divisor = 1;
  131. unsigned long wtcon;
  132. if (timeout < 1)
  133. return -EINVAL;
  134. freq /= 128;
  135. count = timeout * freq;
  136. DBG("%s: count=%d, timeout=%d, freq=%d\n",
  137. __FUNCTION__, count, timeout, freq);
  138. /* if the count is bigger than the watchdog register,
  139. then work out what we need to do (and if) we can
  140. actually make this value
  141. */
  142. if (count >= 0x10000) {
  143. for (divisor = 1; divisor <= 0x100; divisor++) {
  144. if ((count / divisor) < 0x10000)
  145. break;
  146. }
  147. if ((count / divisor) >= 0x10000) {
  148. printk(KERN_ERR PFX "timeout %d too big\n", timeout);
  149. return -EINVAL;
  150. }
  151. }
  152. tmr_margin = timeout;
  153. DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
  154. __FUNCTION__, timeout, divisor, count, count/divisor);
  155. count /= divisor;
  156. wdt_count = count;
  157. /* update the pre-scaler */
  158. wtcon = readl(wdt_base + S3C2410_WTCON);
  159. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  160. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  161. writel(count, wdt_base + S3C2410_WTDAT);
  162. writel(wtcon, wdt_base + S3C2410_WTCON);
  163. return 0;
  164. }
  165. /*
  166. * /dev/watchdog handling
  167. */
  168. static int s3c2410wdt_open(struct inode *inode, struct file *file)
  169. {
  170. if(down_trylock(&open_lock))
  171. return -EBUSY;
  172. if (nowayout) {
  173. __module_get(THIS_MODULE);
  174. } else {
  175. allow_close = CLOSE_STATE_ALLOW;
  176. }
  177. /* start the timer */
  178. s3c2410wdt_start();
  179. return nonseekable_open(inode, file);
  180. }
  181. static int s3c2410wdt_release(struct inode *inode, struct file *file)
  182. {
  183. /*
  184. * Shut off the timer.
  185. * Lock it in if it's a module and we set nowayout
  186. */
  187. if (allow_close == CLOSE_STATE_ALLOW) {
  188. s3c2410wdt_stop();
  189. } else {
  190. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  191. s3c2410wdt_keepalive();
  192. }
  193. allow_close = CLOSE_STATE_NOT;
  194. up(&open_lock);
  195. return 0;
  196. }
  197. static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
  198. size_t len, loff_t *ppos)
  199. {
  200. /*
  201. * Refresh the timer.
  202. */
  203. if(len) {
  204. if (!nowayout) {
  205. size_t i;
  206. /* In case it was set long ago */
  207. allow_close = CLOSE_STATE_NOT;
  208. for (i = 0; i != len; i++) {
  209. char c;
  210. if (get_user(c, data + i))
  211. return -EFAULT;
  212. if (c == 'V')
  213. allow_close = CLOSE_STATE_ALLOW;
  214. }
  215. }
  216. s3c2410wdt_keepalive();
  217. }
  218. return len;
  219. }
  220. #define OPTIONS WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE
  221. static struct watchdog_info s3c2410_wdt_ident = {
  222. .options = OPTIONS,
  223. .firmware_version = 0,
  224. .identity = "S3C2410 Watchdog",
  225. };
  226. static int s3c2410wdt_ioctl(struct inode *inode, struct file *file,
  227. unsigned int cmd, unsigned long arg)
  228. {
  229. void __user *argp = (void __user *)arg;
  230. int __user *p = argp;
  231. int new_margin;
  232. switch (cmd) {
  233. default:
  234. return -ENOIOCTLCMD;
  235. case WDIOC_GETSUPPORT:
  236. return copy_to_user(argp, &s3c2410_wdt_ident,
  237. sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
  238. case WDIOC_GETSTATUS:
  239. case WDIOC_GETBOOTSTATUS:
  240. return put_user(0, p);
  241. case WDIOC_KEEPALIVE:
  242. s3c2410wdt_keepalive();
  243. return 0;
  244. case WDIOC_SETTIMEOUT:
  245. if (get_user(new_margin, p))
  246. return -EFAULT;
  247. if (s3c2410wdt_set_heartbeat(new_margin))
  248. return -EINVAL;
  249. s3c2410wdt_keepalive();
  250. return put_user(tmr_margin, p);
  251. case WDIOC_GETTIMEOUT:
  252. return put_user(tmr_margin, p);
  253. }
  254. }
  255. /*
  256. * Notifier for system down
  257. */
  258. static int s3c2410wdt_notify_sys(struct notifier_block *this, unsigned long code,
  259. void *unused)
  260. {
  261. if(code==SYS_DOWN || code==SYS_HALT) {
  262. /* Turn the WDT off */
  263. s3c2410wdt_stop();
  264. }
  265. return NOTIFY_DONE;
  266. }
  267. /* kernel interface */
  268. static struct file_operations s3c2410wdt_fops = {
  269. .owner = THIS_MODULE,
  270. .llseek = no_llseek,
  271. .write = s3c2410wdt_write,
  272. .ioctl = s3c2410wdt_ioctl,
  273. .open = s3c2410wdt_open,
  274. .release = s3c2410wdt_release,
  275. };
  276. static struct miscdevice s3c2410wdt_miscdev = {
  277. .minor = WATCHDOG_MINOR,
  278. .name = "watchdog",
  279. .fops = &s3c2410wdt_fops,
  280. };
  281. static struct notifier_block s3c2410wdt_notifier = {
  282. .notifier_call = s3c2410wdt_notify_sys,
  283. };
  284. /* interrupt handler code */
  285. static irqreturn_t s3c2410wdt_irq(int irqno, void *param,
  286. struct pt_regs *regs)
  287. {
  288. printk(KERN_INFO PFX "Watchdog timer expired!\n");
  289. s3c2410wdt_keepalive();
  290. return IRQ_HANDLED;
  291. }
  292. /* device interface */
  293. static int s3c2410wdt_probe(struct device *dev)
  294. {
  295. struct platform_device *pdev = to_platform_device(dev);
  296. struct resource *res;
  297. int started = 0;
  298. int ret;
  299. int size;
  300. DBG("%s: probe=%p, device=%p\n", __FUNCTION__, pdev, dev);
  301. /* get the memory region for the watchdog timer */
  302. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  303. if (res == NULL) {
  304. printk(KERN_INFO PFX "failed to get memory region resouce\n");
  305. return -ENOENT;
  306. }
  307. size = (res->end-res->start)+1;
  308. wdt_mem = request_mem_region(res->start, size, pdev->name);
  309. if (wdt_mem == NULL) {
  310. printk(KERN_INFO PFX "failed to get memory region\n");
  311. return -ENOENT;
  312. }
  313. wdt_base = ioremap(res->start, size);
  314. if (wdt_base == 0) {
  315. printk(KERN_INFO PFX "failed to ioremap() region\n");
  316. return -EINVAL;
  317. }
  318. DBG("probe: mapped wdt_base=%p\n", wdt_base);
  319. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  320. if (res == NULL) {
  321. printk(KERN_INFO PFX "failed to get irq resource\n");
  322. return -ENOENT;
  323. }
  324. ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, dev);
  325. if (ret != 0) {
  326. printk(KERN_INFO PFX "failed to install irq (%d)\n", ret);
  327. return ret;
  328. }
  329. wdt_clock = clk_get(dev, "watchdog");
  330. if (wdt_clock == NULL) {
  331. printk(KERN_INFO PFX "failed to find watchdog clock source\n");
  332. return -ENOENT;
  333. }
  334. clk_use(wdt_clock);
  335. clk_enable(wdt_clock);
  336. /* see if we can actually set the requested timer margin, and if
  337. * not, try the default value */
  338. if (s3c2410wdt_set_heartbeat(tmr_margin)) {
  339. started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  340. if (started == 0) {
  341. printk(KERN_INFO PFX "tmr_margin value out of range, default %d used\n",
  342. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  343. } else {
  344. printk(KERN_INFO PFX "default timer value is out of range, cannot start\n");
  345. }
  346. }
  347. ret = register_reboot_notifier(&s3c2410wdt_notifier);
  348. if (ret) {
  349. printk (KERN_ERR PFX "cannot register reboot notifier (%d)\n",
  350. ret);
  351. return ret;
  352. }
  353. ret = misc_register(&s3c2410wdt_miscdev);
  354. if (ret) {
  355. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (%d)\n",
  356. WATCHDOG_MINOR, ret);
  357. unregister_reboot_notifier(&s3c2410wdt_notifier);
  358. return ret;
  359. }
  360. if (tmr_atboot && started == 0) {
  361. printk(KERN_INFO PFX "Starting Watchdog Timer\n");
  362. s3c2410wdt_start();
  363. }
  364. return 0;
  365. }
  366. static int s3c2410wdt_remove(struct device *dev)
  367. {
  368. if (wdt_mem != NULL) {
  369. release_resource(wdt_mem);
  370. kfree(wdt_mem);
  371. wdt_mem = NULL;
  372. }
  373. if (wdt_irq != NULL) {
  374. free_irq(wdt_irq->start, dev);
  375. wdt_irq = NULL;
  376. }
  377. if (wdt_clock != NULL) {
  378. clk_disable(wdt_clock);
  379. clk_unuse(wdt_clock);
  380. clk_put(wdt_clock);
  381. wdt_clock = NULL;
  382. }
  383. misc_deregister(&s3c2410wdt_miscdev);
  384. return 0;
  385. }
  386. static struct device_driver s3c2410wdt_driver = {
  387. .name = "s3c2410-wdt",
  388. .bus = &platform_bus_type,
  389. .probe = s3c2410wdt_probe,
  390. .remove = s3c2410wdt_remove,
  391. };
  392. static char banner[] __initdata = KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
  393. static int __init watchdog_init(void)
  394. {
  395. printk(banner);
  396. return driver_register(&s3c2410wdt_driver);
  397. }
  398. static void __exit watchdog_exit(void)
  399. {
  400. driver_unregister(&s3c2410wdt_driver);
  401. unregister_reboot_notifier(&s3c2410wdt_notifier);
  402. }
  403. module_init(watchdog_init);
  404. module_exit(watchdog_exit);
  405. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  406. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  407. MODULE_LICENSE("GPL");
  408. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);