omap_wdt.c 9.6 KB

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