omap_wdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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@redhat.com>
  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 <mach/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 *armwdt_ck;
  58. struct clk *mpu_wdt_ick;
  59. struct clk *mpu_wdt_fck;
  60. struct resource *mem;
  61. struct miscdevice omap_wdt_miscdev;
  62. };
  63. static void omap_wdt_ping(struct omap_wdt_dev *wdev)
  64. {
  65. void __iomem *base = wdev->base;
  66. /* wait for posted write to complete */
  67. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
  68. cpu_relax();
  69. wdt_trgr_pattern = ~wdt_trgr_pattern;
  70. __raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
  71. /* wait for posted write to complete */
  72. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
  73. cpu_relax();
  74. /* reloaded WCRR from WLDR */
  75. }
  76. static void omap_wdt_enable(struct omap_wdt_dev *wdev)
  77. {
  78. void __iomem *base;
  79. base = wdev->base;
  80. /* Sequence to enable the watchdog */
  81. __raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
  82. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
  83. cpu_relax();
  84. __raw_writel(0x4444, base + OMAP_WATCHDOG_SPR);
  85. while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
  86. cpu_relax();
  87. }
  88. static void omap_wdt_disable(struct omap_wdt_dev *wdev)
  89. {
  90. void __iomem *base;
  91. base = wdev->base;
  92. /* sequence required to disable watchdog */
  93. __raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  94. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
  95. cpu_relax();
  96. __raw_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  97. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
  98. cpu_relax();
  99. }
  100. static void omap_wdt_adjust_timeout(unsigned new_timeout)
  101. {
  102. if (new_timeout < TIMER_MARGIN_MIN)
  103. new_timeout = TIMER_MARGIN_DEFAULT;
  104. if (new_timeout > TIMER_MARGIN_MAX)
  105. new_timeout = TIMER_MARGIN_MAX;
  106. timer_margin = new_timeout;
  107. }
  108. static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev)
  109. {
  110. u32 pre_margin = GET_WLDR_VAL(timer_margin);
  111. void __iomem *base;
  112. base = wdev->base;
  113. /* just count up at 32 KHz */
  114. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
  115. cpu_relax();
  116. __raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
  117. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
  118. cpu_relax();
  119. }
  120. /*
  121. * Allow only one task to hold it open
  122. */
  123. static int omap_wdt_open(struct inode *inode, struct file *file)
  124. {
  125. struct omap_wdt_dev *wdev;
  126. void __iomem *base;
  127. wdev = platform_get_drvdata(omap_wdt_dev);
  128. base = wdev->base;
  129. if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users)))
  130. return -EBUSY;
  131. if (cpu_is_omap16xx())
  132. clk_enable(wdev->armwdt_ck); /* Enable the clock */
  133. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  134. clk_enable(wdev->mpu_wdt_ick); /* Enable the interface clock */
  135. clk_enable(wdev->mpu_wdt_fck); /* Enable the functional clock */
  136. }
  137. /* initialize prescaler */
  138. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
  139. cpu_relax();
  140. __raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
  141. while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
  142. cpu_relax();
  143. file->private_data = (void *) wdev;
  144. omap_wdt_set_timeout(wdev);
  145. omap_wdt_enable(wdev);
  146. return nonseekable_open(inode, file);
  147. }
  148. static int omap_wdt_release(struct inode *inode, struct file *file)
  149. {
  150. struct omap_wdt_dev *wdev;
  151. wdev = file->private_data;
  152. /*
  153. * Shut off the timer unless NOWAYOUT is defined.
  154. */
  155. #ifndef CONFIG_WATCHDOG_NOWAYOUT
  156. omap_wdt_disable(wdev);
  157. if (cpu_is_omap16xx())
  158. clk_disable(wdev->armwdt_ck); /* Disable the clock */
  159. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  160. clk_disable(wdev->mpu_wdt_ick); /* Disable the clock */
  161. clk_disable(wdev->mpu_wdt_fck); /* Disable the clock */
  162. }
  163. #else
  164. printk(KERN_CRIT "omap_wdt: Unexpected close, not stopping!\n");
  165. #endif
  166. wdev->omap_wdt_users = 0;
  167. return 0;
  168. }
  169. static ssize_t omap_wdt_write(struct file *file, const char __user *data,
  170. size_t len, loff_t *ppos)
  171. {
  172. struct omap_wdt_dev *wdev;
  173. wdev = file->private_data;
  174. /* Refresh LOAD_TIME. */
  175. if (len) {
  176. spin_lock(&wdt_lock);
  177. omap_wdt_ping(wdev);
  178. spin_unlock(&wdt_lock);
  179. }
  180. return len;
  181. }
  182. static long omap_wdt_ioctl(struct file *file, unsigned int cmd,
  183. unsigned long arg)
  184. {
  185. struct omap_wdt_dev *wdev;
  186. int new_margin;
  187. static const struct watchdog_info ident = {
  188. .identity = "OMAP Watchdog",
  189. .options = WDIOF_SETTIMEOUT,
  190. .firmware_version = 0,
  191. };
  192. wdev = file->private_data;
  193. switch (cmd) {
  194. case WDIOC_GETSUPPORT:
  195. return copy_to_user((struct watchdog_info __user *)arg, &ident,
  196. sizeof(ident));
  197. case WDIOC_GETSTATUS:
  198. return put_user(0, (int __user *)arg);
  199. case WDIOC_GETBOOTSTATUS:
  200. if (cpu_is_omap16xx())
  201. return put_user(__raw_readw(ARM_SYSST),
  202. (int __user *)arg);
  203. if (cpu_is_omap24xx())
  204. return put_user(omap_prcm_get_reset_sources(),
  205. (int __user *)arg);
  206. case WDIOC_KEEPALIVE:
  207. spin_lock(&wdt_lock);
  208. omap_wdt_ping(wdev);
  209. spin_unlock(&wdt_lock);
  210. return 0;
  211. case WDIOC_SETTIMEOUT:
  212. if (get_user(new_margin, (int __user *)arg))
  213. return -EFAULT;
  214. omap_wdt_adjust_timeout(new_margin);
  215. spin_lock(&wdt_lock);
  216. omap_wdt_disable(wdev);
  217. omap_wdt_set_timeout(wdev);
  218. omap_wdt_enable(wdev);
  219. omap_wdt_ping(wdev);
  220. spin_unlock(&wdt_lock);
  221. /* Fall */
  222. case WDIOC_GETTIMEOUT:
  223. return put_user(timer_margin, (int __user *)arg);
  224. default:
  225. return -ENOTTY;
  226. }
  227. }
  228. static const struct file_operations omap_wdt_fops = {
  229. .owner = THIS_MODULE,
  230. .write = omap_wdt_write,
  231. .unlocked_ioctl = omap_wdt_ioctl,
  232. .open = omap_wdt_open,
  233. .release = omap_wdt_release,
  234. };
  235. static int __init omap_wdt_probe(struct platform_device *pdev)
  236. {
  237. struct resource *res, *mem;
  238. int ret;
  239. struct omap_wdt_dev *wdev;
  240. /* reserve static register mappings */
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (!res)
  243. return -ENOENT;
  244. if (omap_wdt_dev)
  245. return -EBUSY;
  246. mem = request_mem_region(res->start, res->end - res->start + 1,
  247. pdev->name);
  248. if (mem == NULL)
  249. return -EBUSY;
  250. wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL);
  251. if (!wdev) {
  252. ret = -ENOMEM;
  253. goto fail;
  254. }
  255. wdev->omap_wdt_users = 0;
  256. wdev->mem = mem;
  257. if (cpu_is_omap16xx()) {
  258. wdev->armwdt_ck = clk_get(&pdev->dev, "armwdt_ck");
  259. if (IS_ERR(wdev->armwdt_ck)) {
  260. ret = PTR_ERR(wdev->armwdt_ck);
  261. wdev->armwdt_ck = NULL;
  262. goto fail;
  263. }
  264. }
  265. if (cpu_is_omap24xx()) {
  266. wdev->mpu_wdt_ick = clk_get(&pdev->dev, "mpu_wdt_ick");
  267. if (IS_ERR(wdev->mpu_wdt_ick)) {
  268. ret = PTR_ERR(wdev->mpu_wdt_ick);
  269. wdev->mpu_wdt_ick = NULL;
  270. goto fail;
  271. }
  272. wdev->mpu_wdt_fck = clk_get(&pdev->dev, "mpu_wdt_fck");
  273. if (IS_ERR(wdev->mpu_wdt_fck)) {
  274. ret = PTR_ERR(wdev->mpu_wdt_fck);
  275. wdev->mpu_wdt_fck = NULL;
  276. goto fail;
  277. }
  278. }
  279. if (cpu_is_omap34xx()) {
  280. wdev->mpu_wdt_ick = clk_get(&pdev->dev, "wdt2_ick");
  281. if (IS_ERR(wdev->mpu_wdt_ick)) {
  282. ret = PTR_ERR(wdev->mpu_wdt_ick);
  283. wdev->mpu_wdt_ick = NULL;
  284. goto fail;
  285. }
  286. wdev->mpu_wdt_fck = clk_get(&pdev->dev, "wdt2_fck");
  287. if (IS_ERR(wdev->mpu_wdt_fck)) {
  288. ret = PTR_ERR(wdev->mpu_wdt_fck);
  289. wdev->mpu_wdt_fck = NULL;
  290. goto fail;
  291. }
  292. }
  293. wdev->base = ioremap(res->start, res->end - res->start + 1);
  294. if (!wdev->base) {
  295. ret = -ENOMEM;
  296. goto fail;
  297. }
  298. platform_set_drvdata(pdev, wdev);
  299. omap_wdt_disable(wdev);
  300. omap_wdt_adjust_timeout(timer_margin);
  301. wdev->omap_wdt_miscdev.parent = &pdev->dev;
  302. wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR;
  303. wdev->omap_wdt_miscdev.name = "watchdog";
  304. wdev->omap_wdt_miscdev.fops = &omap_wdt_fops;
  305. ret = misc_register(&(wdev->omap_wdt_miscdev));
  306. if (ret)
  307. goto fail;
  308. pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
  309. __raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
  310. timer_margin);
  311. /* autogate OCP interface clock */
  312. __raw_writel(0x01, wdev->base + OMAP_WATCHDOG_SYS_CONFIG);
  313. omap_wdt_dev = pdev;
  314. return 0;
  315. fail:
  316. if (wdev) {
  317. platform_set_drvdata(pdev, NULL);
  318. if (wdev->armwdt_ck)
  319. clk_put(wdev->armwdt_ck);
  320. if (wdev->mpu_wdt_ick)
  321. clk_put(wdev->mpu_wdt_ick);
  322. if (wdev->mpu_wdt_fck)
  323. clk_put(wdev->mpu_wdt_fck);
  324. iounmap(wdev->base);
  325. kfree(wdev);
  326. }
  327. if (mem) {
  328. release_mem_region(res->start, res->end - res->start + 1);
  329. }
  330. return ret;
  331. }
  332. static void omap_wdt_shutdown(struct platform_device *pdev)
  333. {
  334. struct omap_wdt_dev *wdev;
  335. wdev = platform_get_drvdata(pdev);
  336. if (wdev->omap_wdt_users)
  337. omap_wdt_disable(wdev);
  338. }
  339. static int omap_wdt_remove(struct platform_device *pdev)
  340. {
  341. struct omap_wdt_dev *wdev;
  342. wdev = platform_get_drvdata(pdev);
  343. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  344. if (!res)
  345. return -ENOENT;
  346. misc_deregister(&(wdev->omap_wdt_miscdev));
  347. release_mem_region(res->start, res->end - res->start + 1);
  348. platform_set_drvdata(pdev, NULL);
  349. if (wdev->armwdt_ck) {
  350. clk_put(wdev->armwdt_ck);
  351. wdev->armwdt_ck = NULL;
  352. }
  353. if (wdev->mpu_wdt_ick) {
  354. clk_put(wdev->mpu_wdt_ick);
  355. wdev->mpu_wdt_ick = NULL;
  356. }
  357. if (wdev->mpu_wdt_fck) {
  358. clk_put(wdev->mpu_wdt_fck);
  359. wdev->mpu_wdt_fck = NULL;
  360. }
  361. iounmap(wdev->base);
  362. kfree(wdev);
  363. omap_wdt_dev = NULL;
  364. return 0;
  365. }
  366. #ifdef CONFIG_PM
  367. /* REVISIT ... not clear this is the best way to handle system suspend; and
  368. * it's very inappropriate for selective device suspend (e.g. suspending this
  369. * through sysfs rather than by stopping the watchdog daemon). Also, this
  370. * may not play well enough with NOWAYOUT...
  371. */
  372. static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  373. {
  374. struct omap_wdt_dev *wdev;
  375. wdev = platform_get_drvdata(pdev);
  376. if (wdev->omap_wdt_users)
  377. omap_wdt_disable(wdev);
  378. return 0;
  379. }
  380. static int omap_wdt_resume(struct platform_device *pdev)
  381. {
  382. struct omap_wdt_dev *wdev;
  383. wdev = platform_get_drvdata(pdev);
  384. if (wdev->omap_wdt_users) {
  385. omap_wdt_enable(wdev);
  386. omap_wdt_ping(wdev);
  387. }
  388. return 0;
  389. }
  390. #else
  391. #define omap_wdt_suspend NULL
  392. #define omap_wdt_resume NULL
  393. #endif
  394. static struct platform_driver omap_wdt_driver = {
  395. .probe = omap_wdt_probe,
  396. .remove = omap_wdt_remove,
  397. .shutdown = omap_wdt_shutdown,
  398. .suspend = omap_wdt_suspend,
  399. .resume = omap_wdt_resume,
  400. .driver = {
  401. .owner = THIS_MODULE,
  402. .name = "omap_wdt",
  403. },
  404. };
  405. static int __init omap_wdt_init(void)
  406. {
  407. spin_lock_init(&wdt_lock);
  408. return platform_driver_register(&omap_wdt_driver);
  409. }
  410. static void __exit omap_wdt_exit(void)
  411. {
  412. platform_driver_unregister(&omap_wdt_driver);
  413. }
  414. module_init(omap_wdt_init);
  415. module_exit(omap_wdt_exit);
  416. MODULE_AUTHOR("George G. Davis");
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  419. MODULE_ALIAS("platform:omap_wdt");