omap_wdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * omap_wdt.c
  3. *
  4. * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * <gdavis@mvista.com> or <source@mvista.com>
  8. *
  9. * 2003 (c) MontaVista Software, Inc. This file is licensed under the
  10. * terms of the GNU General Public License version 2. This program is
  11. * licensed "as is" without any warranty of any kind, whether express
  12. * or implied.
  13. *
  14. * History:
  15. *
  16. * 20030527: George G. Davis <gdavis@mvista.com>
  17. * Initially based on linux-2.4.19-rmk7-pxa1/drivers/char/sa1100_wdt.c
  18. * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
  19. * Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk>
  20. *
  21. * Copyright (c) 2004 Texas Instruments.
  22. * 1. Modified to support OMAP1610 32-KHz watchdog timer
  23. * 2. Ported to 2.6 kernel
  24. *
  25. * Copyright (c) 2005 David Brownell
  26. * Use the driver model and standard identifiers; handle bigger timeouts.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/types.h>
  30. #include <linux/kernel.h>
  31. #include <linux/fs.h>
  32. #include <linux/mm.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/watchdog.h>
  35. #include <linux/reboot.h>
  36. #include <linux/init.h>
  37. #include <linux/err.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/clk.h>
  41. #include <linux/bitops.h>
  42. #include <linux/io.h>
  43. #include <linux/uaccess.h>
  44. #include <mach/hardware.h>
  45. #include <plat/prcm.h>
  46. #include "omap_wdt.h"
  47. static struct platform_device *omap_wdt_dev;
  48. static unsigned timer_margin;
  49. module_param(timer_margin, uint, 0);
  50. MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)");
  51. static unsigned int wdt_trgr_pattern = 0x1234;
  52. static spinlock_t wdt_lock;
  53. struct omap_wdt_dev {
  54. void __iomem *base; /* physical */
  55. struct device *dev;
  56. int omap_wdt_users;
  57. struct clk *ick;
  58. struct clk *fck;
  59. struct resource *mem;
  60. struct miscdevice omap_wdt_miscdev;
  61. };
  62. static void omap_wdt_ping(struct omap_wdt_dev *wdev)
  63. {
  64. void __iomem *base = wdev->base;
  65. /* wait for posted write to complete */
  66. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
  67. cpu_relax();
  68. wdt_trgr_pattern = ~wdt_trgr_pattern;
  69. __raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
  70. /* wait for posted write to complete */
  71. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
  72. cpu_relax();
  73. /* reloaded WCRR from WLDR */
  74. }
  75. static void omap_wdt_enable(struct omap_wdt_dev *wdev)
  76. {
  77. void __iomem *base = wdev->base;
  78. /* Sequence to enable the watchdog */
  79. __raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
  80. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
  81. cpu_relax();
  82. __raw_writel(0x4444, base + OMAP_WATCHDOG_SPR);
  83. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
  84. cpu_relax();
  85. }
  86. static void omap_wdt_disable(struct omap_wdt_dev *wdev)
  87. {
  88. void __iomem *base = wdev->base;
  89. /* sequence required to disable watchdog */
  90. __raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  91. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
  92. cpu_relax();
  93. __raw_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  94. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
  95. cpu_relax();
  96. }
  97. static void omap_wdt_adjust_timeout(unsigned new_timeout)
  98. {
  99. if (new_timeout < TIMER_MARGIN_MIN)
  100. new_timeout = TIMER_MARGIN_DEFAULT;
  101. if (new_timeout > TIMER_MARGIN_MAX)
  102. new_timeout = TIMER_MARGIN_MAX;
  103. timer_margin = new_timeout;
  104. }
  105. static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev)
  106. {
  107. u32 pre_margin = GET_WLDR_VAL(timer_margin);
  108. void __iomem *base = wdev->base;
  109. /* just count up at 32 KHz */
  110. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
  111. cpu_relax();
  112. __raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
  113. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
  114. cpu_relax();
  115. }
  116. /*
  117. * Allow only one task to hold it open
  118. */
  119. static int omap_wdt_open(struct inode *inode, struct file *file)
  120. {
  121. struct omap_wdt_dev *wdev = platform_get_drvdata(omap_wdt_dev);
  122. void __iomem *base = wdev->base;
  123. if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users)))
  124. return -EBUSY;
  125. clk_enable(wdev->ick); /* Enable the interface clock */
  126. clk_enable(wdev->fck); /* Enable the functional clock */
  127. /* initialize prescaler */
  128. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
  129. cpu_relax();
  130. __raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
  131. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
  132. cpu_relax();
  133. file->private_data = (void *) wdev;
  134. omap_wdt_set_timeout(wdev);
  135. omap_wdt_ping(wdev); /* trigger loading of new timeout value */
  136. omap_wdt_enable(wdev);
  137. return nonseekable_open(inode, file);
  138. }
  139. static int omap_wdt_release(struct inode *inode, struct file *file)
  140. {
  141. struct omap_wdt_dev *wdev = file->private_data;
  142. /*
  143. * Shut off the timer unless NOWAYOUT is defined.
  144. */
  145. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  146. omap_wdt_disable(wdev);
  147. clk_disable(wdev->ick);
  148. clk_disable(wdev->fck);
  149. #else
  150. printk(KERN_CRIT "omap_wdt: Unexpected close, not stopping!\n");
  151. #endif
  152. wdev->omap_wdt_users = 0;
  153. return 0;
  154. }
  155. static ssize_t omap_wdt_write(struct file *file, const char __user *data,
  156. size_t len, loff_t *ppos)
  157. {
  158. struct omap_wdt_dev *wdev = file->private_data;
  159. /* Refresh LOAD_TIME. */
  160. if (len) {
  161. spin_lock(&wdt_lock);
  162. omap_wdt_ping(wdev);
  163. spin_unlock(&wdt_lock);
  164. }
  165. return len;
  166. }
  167. static long omap_wdt_ioctl(struct file *file, unsigned int cmd,
  168. unsigned long arg)
  169. {
  170. struct omap_wdt_dev *wdev;
  171. int new_margin;
  172. static const struct watchdog_info ident = {
  173. .identity = "OMAP Watchdog",
  174. .options = WDIOF_SETTIMEOUT,
  175. .firmware_version = 0,
  176. };
  177. wdev = file->private_data;
  178. switch (cmd) {
  179. case WDIOC_GETSUPPORT:
  180. return copy_to_user((struct watchdog_info __user *)arg, &ident,
  181. sizeof(ident));
  182. case WDIOC_GETSTATUS:
  183. return put_user(0, (int __user *)arg);
  184. case WDIOC_GETBOOTSTATUS:
  185. if (cpu_is_omap16xx())
  186. return put_user(__raw_readw(ARM_SYSST),
  187. (int __user *)arg);
  188. if (cpu_is_omap24xx())
  189. return put_user(omap_prcm_get_reset_sources(),
  190. (int __user *)arg);
  191. case WDIOC_KEEPALIVE:
  192. spin_lock(&wdt_lock);
  193. omap_wdt_ping(wdev);
  194. spin_unlock(&wdt_lock);
  195. return 0;
  196. case WDIOC_SETTIMEOUT:
  197. if (get_user(new_margin, (int __user *)arg))
  198. return -EFAULT;
  199. omap_wdt_adjust_timeout(new_margin);
  200. spin_lock(&wdt_lock);
  201. omap_wdt_disable(wdev);
  202. omap_wdt_set_timeout(wdev);
  203. omap_wdt_enable(wdev);
  204. omap_wdt_ping(wdev);
  205. spin_unlock(&wdt_lock);
  206. /* Fall */
  207. case WDIOC_GETTIMEOUT:
  208. return put_user(timer_margin, (int __user *)arg);
  209. default:
  210. return -ENOTTY;
  211. }
  212. }
  213. static const struct file_operations omap_wdt_fops = {
  214. .owner = THIS_MODULE,
  215. .write = omap_wdt_write,
  216. .unlocked_ioctl = omap_wdt_ioctl,
  217. .open = omap_wdt_open,
  218. .release = omap_wdt_release,
  219. };
  220. static int __devinit omap_wdt_probe(struct platform_device *pdev)
  221. {
  222. struct resource *res, *mem;
  223. struct omap_wdt_dev *wdev;
  224. int ret;
  225. /* reserve static register mappings */
  226. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  227. if (!res) {
  228. ret = -ENOENT;
  229. goto err_get_resource;
  230. }
  231. if (omap_wdt_dev) {
  232. ret = -EBUSY;
  233. goto err_busy;
  234. }
  235. mem = request_mem_region(res->start, resource_size(res), pdev->name);
  236. if (!mem) {
  237. ret = -EBUSY;
  238. goto err_busy;
  239. }
  240. wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL);
  241. if (!wdev) {
  242. ret = -ENOMEM;
  243. goto err_kzalloc;
  244. }
  245. wdev->omap_wdt_users = 0;
  246. wdev->mem = mem;
  247. wdev->ick = clk_get(&pdev->dev, "ick");
  248. if (IS_ERR(wdev->ick)) {
  249. ret = PTR_ERR(wdev->ick);
  250. wdev->ick = NULL;
  251. goto err_clk;
  252. }
  253. wdev->fck = clk_get(&pdev->dev, "fck");
  254. if (IS_ERR(wdev->fck)) {
  255. ret = PTR_ERR(wdev->fck);
  256. wdev->fck = NULL;
  257. goto err_clk;
  258. }
  259. wdev->base = ioremap(res->start, resource_size(res));
  260. if (!wdev->base) {
  261. ret = -ENOMEM;
  262. goto err_ioremap;
  263. }
  264. platform_set_drvdata(pdev, wdev);
  265. clk_enable(wdev->ick);
  266. clk_enable(wdev->fck);
  267. omap_wdt_disable(wdev);
  268. omap_wdt_adjust_timeout(timer_margin);
  269. wdev->omap_wdt_miscdev.parent = &pdev->dev;
  270. wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR;
  271. wdev->omap_wdt_miscdev.name = "watchdog";
  272. wdev->omap_wdt_miscdev.fops = &omap_wdt_fops;
  273. ret = misc_register(&(wdev->omap_wdt_miscdev));
  274. if (ret)
  275. goto err_misc;
  276. pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
  277. __raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
  278. timer_margin);
  279. /* autogate OCP interface clock */
  280. __raw_writel(0x01, wdev->base + OMAP_WATCHDOG_SYS_CONFIG);
  281. clk_disable(wdev->ick);
  282. clk_disable(wdev->fck);
  283. omap_wdt_dev = pdev;
  284. return 0;
  285. err_misc:
  286. platform_set_drvdata(pdev, NULL);
  287. iounmap(wdev->base);
  288. err_ioremap:
  289. wdev->base = NULL;
  290. err_clk:
  291. if (wdev->ick)
  292. clk_put(wdev->ick);
  293. if (wdev->fck)
  294. clk_put(wdev->fck);
  295. kfree(wdev);
  296. err_kzalloc:
  297. release_mem_region(res->start, resource_size(res));
  298. err_busy:
  299. err_get_resource:
  300. return ret;
  301. }
  302. static void omap_wdt_shutdown(struct platform_device *pdev)
  303. {
  304. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  305. if (wdev->omap_wdt_users)
  306. omap_wdt_disable(wdev);
  307. }
  308. static int __devexit omap_wdt_remove(struct platform_device *pdev)
  309. {
  310. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  311. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. if (!res)
  313. return -ENOENT;
  314. misc_deregister(&(wdev->omap_wdt_miscdev));
  315. release_mem_region(res->start, resource_size(res));
  316. platform_set_drvdata(pdev, NULL);
  317. clk_put(wdev->ick);
  318. clk_put(wdev->fck);
  319. iounmap(wdev->base);
  320. kfree(wdev);
  321. omap_wdt_dev = NULL;
  322. return 0;
  323. }
  324. #ifdef CONFIG_PM
  325. /* REVISIT ... not clear this is the best way to handle system suspend; and
  326. * it's very inappropriate for selective device suspend (e.g. suspending this
  327. * through sysfs rather than by stopping the watchdog daemon). Also, this
  328. * may not play well enough with NOWAYOUT...
  329. */
  330. static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  331. {
  332. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  333. if (wdev->omap_wdt_users)
  334. omap_wdt_disable(wdev);
  335. return 0;
  336. }
  337. static int omap_wdt_resume(struct platform_device *pdev)
  338. {
  339. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  340. if (wdev->omap_wdt_users) {
  341. omap_wdt_enable(wdev);
  342. omap_wdt_ping(wdev);
  343. }
  344. return 0;
  345. }
  346. #else
  347. #define omap_wdt_suspend NULL
  348. #define omap_wdt_resume NULL
  349. #endif
  350. static struct platform_driver omap_wdt_driver = {
  351. .probe = omap_wdt_probe,
  352. .remove = __devexit_p(omap_wdt_remove),
  353. .shutdown = omap_wdt_shutdown,
  354. .suspend = omap_wdt_suspend,
  355. .resume = omap_wdt_resume,
  356. .driver = {
  357. .owner = THIS_MODULE,
  358. .name = "omap_wdt",
  359. },
  360. };
  361. static int __init omap_wdt_init(void)
  362. {
  363. spin_lock_init(&wdt_lock);
  364. return platform_driver_register(&omap_wdt_driver);
  365. }
  366. static void __exit omap_wdt_exit(void)
  367. {
  368. platform_driver_unregister(&omap_wdt_driver);
  369. }
  370. module_init(omap_wdt_init);
  371. module_exit(omap_wdt_exit);
  372. MODULE_AUTHOR("George G. Davis");
  373. MODULE_LICENSE("GPL");
  374. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  375. MODULE_ALIAS("platform:omap_wdt");