omap_wdt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/mm.h>
  33. #include <linux/watchdog.h>
  34. #include <linux/reboot.h>
  35. #include <linux/err.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/io.h>
  39. #include <linux/slab.h>
  40. #include <linux/pm_runtime.h>
  41. #include <linux/platform_data/omap-wd-timer.h>
  42. #include "omap_wdt.h"
  43. static bool nowayout = WATCHDOG_NOWAYOUT;
  44. module_param(nowayout, bool, 0);
  45. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  46. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  47. static unsigned timer_margin;
  48. module_param(timer_margin, uint, 0);
  49. MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)");
  50. #define to_omap_wdt_dev(_wdog) container_of(_wdog, struct omap_wdt_dev, wdog)
  51. static bool early_enable;
  52. module_param(early_enable, bool, 0);
  53. MODULE_PARM_DESC(early_enable,
  54. "Watchdog is started on module insertion (default=0)");
  55. struct omap_wdt_dev {
  56. struct watchdog_device wdog;
  57. void __iomem *base; /* physical */
  58. struct device *dev;
  59. bool omap_wdt_users;
  60. int wdt_trgr_pattern;
  61. struct mutex lock; /* to avoid races with PM */
  62. };
  63. static void omap_wdt_reload(struct omap_wdt_dev *wdev)
  64. {
  65. void __iomem *base = wdev->base;
  66. /* wait for posted write to complete */
  67. while ((readl_relaxed(base + OMAP_WATCHDOG_WPS)) & 0x08)
  68. cpu_relax();
  69. wdev->wdt_trgr_pattern = ~wdev->wdt_trgr_pattern;
  70. writel_relaxed(wdev->wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
  71. /* wait for posted write to complete */
  72. while ((readl_relaxed(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. writel_relaxed(0xBBBB, base + OMAP_WATCHDOG_SPR);
  81. while ((readl_relaxed(base + OMAP_WATCHDOG_WPS)) & 0x10)
  82. cpu_relax();
  83. writel_relaxed(0x4444, base + OMAP_WATCHDOG_SPR);
  84. while ((readl_relaxed(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. writel_relaxed(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  92. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x10)
  93. cpu_relax();
  94. writel_relaxed(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
  95. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x10)
  96. cpu_relax();
  97. }
  98. static void omap_wdt_set_timer(struct omap_wdt_dev *wdev,
  99. unsigned int timeout)
  100. {
  101. u32 pre_margin = GET_WLDR_VAL(timeout);
  102. void __iomem *base = wdev->base;
  103. /* just count up at 32 KHz */
  104. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x04)
  105. cpu_relax();
  106. writel_relaxed(pre_margin, base + OMAP_WATCHDOG_LDR);
  107. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x04)
  108. cpu_relax();
  109. }
  110. static int omap_wdt_start(struct watchdog_device *wdog)
  111. {
  112. struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
  113. void __iomem *base = wdev->base;
  114. mutex_lock(&wdev->lock);
  115. wdev->omap_wdt_users = true;
  116. pm_runtime_get_sync(wdev->dev);
  117. /*
  118. * Make sure the watchdog is disabled. This is unfortunately required
  119. * because writing to various registers with the watchdog running has no
  120. * effect.
  121. */
  122. omap_wdt_disable(wdev);
  123. /* initialize prescaler */
  124. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x01)
  125. cpu_relax();
  126. writel_relaxed((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
  127. while (readl_relaxed(base + OMAP_WATCHDOG_WPS) & 0x01)
  128. cpu_relax();
  129. omap_wdt_set_timer(wdev, wdog->timeout);
  130. omap_wdt_reload(wdev); /* trigger loading of new timeout value */
  131. omap_wdt_enable(wdev);
  132. mutex_unlock(&wdev->lock);
  133. return 0;
  134. }
  135. static int omap_wdt_stop(struct watchdog_device *wdog)
  136. {
  137. struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
  138. mutex_lock(&wdev->lock);
  139. omap_wdt_disable(wdev);
  140. pm_runtime_put_sync(wdev->dev);
  141. wdev->omap_wdt_users = false;
  142. mutex_unlock(&wdev->lock);
  143. return 0;
  144. }
  145. static int omap_wdt_ping(struct watchdog_device *wdog)
  146. {
  147. struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
  148. mutex_lock(&wdev->lock);
  149. omap_wdt_reload(wdev);
  150. mutex_unlock(&wdev->lock);
  151. return 0;
  152. }
  153. static int omap_wdt_set_timeout(struct watchdog_device *wdog,
  154. unsigned int timeout)
  155. {
  156. struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
  157. mutex_lock(&wdev->lock);
  158. omap_wdt_disable(wdev);
  159. omap_wdt_set_timer(wdev, timeout);
  160. omap_wdt_enable(wdev);
  161. omap_wdt_reload(wdev);
  162. wdog->timeout = timeout;
  163. mutex_unlock(&wdev->lock);
  164. return 0;
  165. }
  166. static unsigned int omap_wdt_get_timeleft(struct watchdog_device *wdog)
  167. {
  168. struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
  169. void __iomem *base = wdev->base;
  170. u32 value;
  171. value = readl_relaxed(base + OMAP_WATCHDOG_CRR);
  172. return GET_WCCR_SECS(value);
  173. }
  174. static const struct watchdog_info omap_wdt_info = {
  175. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  176. .identity = "OMAP Watchdog",
  177. };
  178. static const struct watchdog_ops omap_wdt_ops = {
  179. .owner = THIS_MODULE,
  180. .start = omap_wdt_start,
  181. .stop = omap_wdt_stop,
  182. .ping = omap_wdt_ping,
  183. .set_timeout = omap_wdt_set_timeout,
  184. .get_timeleft = omap_wdt_get_timeleft,
  185. };
  186. static int omap_wdt_probe(struct platform_device *pdev)
  187. {
  188. struct omap_wd_timer_platform_data *pdata = dev_get_platdata(&pdev->dev);
  189. struct resource *res;
  190. struct omap_wdt_dev *wdev;
  191. int ret;
  192. wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
  193. if (!wdev)
  194. return -ENOMEM;
  195. wdev->omap_wdt_users = false;
  196. wdev->dev = &pdev->dev;
  197. wdev->wdt_trgr_pattern = 0x1234;
  198. mutex_init(&wdev->lock);
  199. /* reserve static register mappings */
  200. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  201. wdev->base = devm_ioremap_resource(&pdev->dev, res);
  202. if (IS_ERR(wdev->base))
  203. return PTR_ERR(wdev->base);
  204. wdev->wdog.info = &omap_wdt_info;
  205. wdev->wdog.ops = &omap_wdt_ops;
  206. wdev->wdog.min_timeout = TIMER_MARGIN_MIN;
  207. wdev->wdog.max_timeout = TIMER_MARGIN_MAX;
  208. wdev->wdog.parent = &pdev->dev;
  209. if (watchdog_init_timeout(&wdev->wdog, timer_margin, &pdev->dev) < 0)
  210. wdev->wdog.timeout = TIMER_MARGIN_DEFAULT;
  211. watchdog_set_nowayout(&wdev->wdog, nowayout);
  212. platform_set_drvdata(pdev, wdev);
  213. pm_runtime_enable(wdev->dev);
  214. pm_runtime_get_sync(wdev->dev);
  215. if (pdata && pdata->read_reset_sources) {
  216. u32 rs = pdata->read_reset_sources();
  217. if (rs & (1 << OMAP_MPU_WD_RST_SRC_ID_SHIFT))
  218. wdev->wdog.bootstatus = WDIOF_CARDRESET;
  219. }
  220. omap_wdt_disable(wdev);
  221. ret = watchdog_register_device(&wdev->wdog);
  222. if (ret) {
  223. pm_runtime_disable(wdev->dev);
  224. return ret;
  225. }
  226. pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
  227. readl_relaxed(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
  228. wdev->wdog.timeout);
  229. pm_runtime_put_sync(wdev->dev);
  230. if (early_enable)
  231. omap_wdt_start(&wdev->wdog);
  232. return 0;
  233. }
  234. static void omap_wdt_shutdown(struct platform_device *pdev)
  235. {
  236. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  237. mutex_lock(&wdev->lock);
  238. if (wdev->omap_wdt_users) {
  239. omap_wdt_disable(wdev);
  240. pm_runtime_put_sync(wdev->dev);
  241. }
  242. mutex_unlock(&wdev->lock);
  243. }
  244. static int omap_wdt_remove(struct platform_device *pdev)
  245. {
  246. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  247. pm_runtime_disable(wdev->dev);
  248. watchdog_unregister_device(&wdev->wdog);
  249. return 0;
  250. }
  251. #ifdef CONFIG_PM
  252. /* REVISIT ... not clear this is the best way to handle system suspend; and
  253. * it's very inappropriate for selective device suspend (e.g. suspending this
  254. * through sysfs rather than by stopping the watchdog daemon). Also, this
  255. * may not play well enough with NOWAYOUT...
  256. */
  257. static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
  258. {
  259. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  260. mutex_lock(&wdev->lock);
  261. if (wdev->omap_wdt_users) {
  262. omap_wdt_disable(wdev);
  263. pm_runtime_put_sync(wdev->dev);
  264. }
  265. mutex_unlock(&wdev->lock);
  266. return 0;
  267. }
  268. static int omap_wdt_resume(struct platform_device *pdev)
  269. {
  270. struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
  271. mutex_lock(&wdev->lock);
  272. if (wdev->omap_wdt_users) {
  273. pm_runtime_get_sync(wdev->dev);
  274. omap_wdt_enable(wdev);
  275. omap_wdt_reload(wdev);
  276. }
  277. mutex_unlock(&wdev->lock);
  278. return 0;
  279. }
  280. #else
  281. #define omap_wdt_suspend NULL
  282. #define omap_wdt_resume NULL
  283. #endif
  284. static const struct of_device_id omap_wdt_of_match[] = {
  285. { .compatible = "ti,omap3-wdt", },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, omap_wdt_of_match);
  289. static struct platform_driver omap_wdt_driver = {
  290. .probe = omap_wdt_probe,
  291. .remove = omap_wdt_remove,
  292. .shutdown = omap_wdt_shutdown,
  293. .suspend = omap_wdt_suspend,
  294. .resume = omap_wdt_resume,
  295. .driver = {
  296. .name = "omap_wdt",
  297. .of_match_table = omap_wdt_of_match,
  298. },
  299. };
  300. module_platform_driver(omap_wdt_driver);
  301. MODULE_AUTHOR("George G. Davis");
  302. MODULE_LICENSE("GPL");
  303. MODULE_ALIAS("platform:omap_wdt");