omap_wdt.c 12 KB

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