4 * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog
6 * Author: MontaVista Software, Inc.
7 * <gdavis@mvista.com> or <source@mvista.com>
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
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>
21 * Copyright (c) 2004 Texas Instruments.
22 * 1. Modified to support OMAP1610 32-KHz watchdog timer
23 * 2. Ported to 2.6 kernel
25 * Copyright (c) 2005 David Brownell
26 * Use the driver model and standard identifiers; handle bigger timeouts.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/kernel.h>
35 #include <linux/watchdog.h>
36 #include <linux/reboot.h>
37 #include <linux/err.h>
38 #include <linux/platform_device.h>
39 #include <linux/moduleparam.h>
41 #include <linux/slab.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/platform_data/omap-wd-timer.h>
47 static bool nowayout
= WATCHDOG_NOWAYOUT
;
48 module_param(nowayout
, bool, 0);
49 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
50 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
52 static unsigned timer_margin
;
53 module_param(timer_margin
, uint
, 0);
54 MODULE_PARM_DESC(timer_margin
, "initial watchdog timeout (in seconds)");
56 #define to_omap_wdt_dev(_wdog) container_of(_wdog, struct omap_wdt_dev, wdog)
59 struct watchdog_device wdog
;
60 void __iomem
*base
; /* physical */
64 struct mutex lock
; /* to avoid races with PM */
67 static void omap_wdt_reload(struct omap_wdt_dev
*wdev
)
69 void __iomem
*base
= wdev
->base
;
71 /* wait for posted write to complete */
72 while ((readl_relaxed(base
+ OMAP_WATCHDOG_WPS
)) & 0x08)
75 wdev
->wdt_trgr_pattern
= ~wdev
->wdt_trgr_pattern
;
76 writel_relaxed(wdev
->wdt_trgr_pattern
, (base
+ OMAP_WATCHDOG_TGR
));
78 /* wait for posted write to complete */
79 while ((readl_relaxed(base
+ OMAP_WATCHDOG_WPS
)) & 0x08)
81 /* reloaded WCRR from WLDR */
84 static void omap_wdt_enable(struct omap_wdt_dev
*wdev
)
86 void __iomem
*base
= wdev
->base
;
88 /* Sequence to enable the watchdog */
89 writel_relaxed(0xBBBB, base
+ OMAP_WATCHDOG_SPR
);
90 while ((readl_relaxed(base
+ OMAP_WATCHDOG_WPS
)) & 0x10)
93 writel_relaxed(0x4444, base
+ OMAP_WATCHDOG_SPR
);
94 while ((readl_relaxed(base
+ OMAP_WATCHDOG_WPS
)) & 0x10)
98 static void omap_wdt_disable(struct omap_wdt_dev
*wdev
)
100 void __iomem
*base
= wdev
->base
;
102 /* sequence required to disable watchdog */
103 writel_relaxed(0xAAAA, base
+ OMAP_WATCHDOG_SPR
); /* TIMER_MODE */
104 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x10)
107 writel_relaxed(0x5555, base
+ OMAP_WATCHDOG_SPR
); /* TIMER_MODE */
108 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x10)
112 static void omap_wdt_set_timer(struct omap_wdt_dev
*wdev
,
113 unsigned int timeout
)
115 u32 pre_margin
= GET_WLDR_VAL(timeout
);
116 void __iomem
*base
= wdev
->base
;
118 /* just count up at 32 KHz */
119 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x04)
122 writel_relaxed(pre_margin
, base
+ OMAP_WATCHDOG_LDR
);
123 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x04)
127 static int omap_wdt_start(struct watchdog_device
*wdog
)
129 struct omap_wdt_dev
*wdev
= to_omap_wdt_dev(wdog
);
130 void __iomem
*base
= wdev
->base
;
132 mutex_lock(&wdev
->lock
);
134 wdev
->omap_wdt_users
= true;
136 pm_runtime_get_sync(wdev
->dev
);
139 * Make sure the watchdog is disabled. This is unfortunately required
140 * because writing to various registers with the watchdog running has no
143 omap_wdt_disable(wdev
);
145 /* initialize prescaler */
146 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x01)
149 writel_relaxed((1 << 5) | (PTV
<< 2), base
+ OMAP_WATCHDOG_CNTRL
);
150 while (readl_relaxed(base
+ OMAP_WATCHDOG_WPS
) & 0x01)
153 omap_wdt_set_timer(wdev
, wdog
->timeout
);
154 omap_wdt_reload(wdev
); /* trigger loading of new timeout value */
155 omap_wdt_enable(wdev
);
157 mutex_unlock(&wdev
->lock
);
162 static int omap_wdt_stop(struct watchdog_device
*wdog
)
164 struct omap_wdt_dev
*wdev
= to_omap_wdt_dev(wdog
);
166 mutex_lock(&wdev
->lock
);
167 omap_wdt_disable(wdev
);
168 pm_runtime_put_sync(wdev
->dev
);
169 wdev
->omap_wdt_users
= false;
170 mutex_unlock(&wdev
->lock
);
174 static int omap_wdt_ping(struct watchdog_device
*wdog
)
176 struct omap_wdt_dev
*wdev
= to_omap_wdt_dev(wdog
);
178 mutex_lock(&wdev
->lock
);
179 omap_wdt_reload(wdev
);
180 mutex_unlock(&wdev
->lock
);
185 static int omap_wdt_set_timeout(struct watchdog_device
*wdog
,
186 unsigned int timeout
)
188 struct omap_wdt_dev
*wdev
= to_omap_wdt_dev(wdog
);
190 mutex_lock(&wdev
->lock
);
191 omap_wdt_disable(wdev
);
192 omap_wdt_set_timer(wdev
, timeout
);
193 omap_wdt_enable(wdev
);
194 omap_wdt_reload(wdev
);
195 wdog
->timeout
= timeout
;
196 mutex_unlock(&wdev
->lock
);
201 static unsigned int omap_wdt_get_timeleft(struct watchdog_device
*wdog
)
203 struct omap_wdt_dev
*wdev
= watchdog_get_drvdata(wdog
);
204 void __iomem
*base
= wdev
->base
;
207 value
= readl_relaxed(base
+ OMAP_WATCHDOG_CRR
);
208 return GET_WCCR_SECS(value
);
211 static const struct watchdog_info omap_wdt_info
= {
212 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
213 .identity
= "OMAP Watchdog",
216 static const struct watchdog_ops omap_wdt_ops
= {
217 .owner
= THIS_MODULE
,
218 .start
= omap_wdt_start
,
219 .stop
= omap_wdt_stop
,
220 .ping
= omap_wdt_ping
,
221 .set_timeout
= omap_wdt_set_timeout
,
222 .get_timeleft
= omap_wdt_get_timeleft
,
225 static int omap_wdt_probe(struct platform_device
*pdev
)
227 struct omap_wd_timer_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
228 struct resource
*res
;
229 struct omap_wdt_dev
*wdev
;
232 wdev
= devm_kzalloc(&pdev
->dev
, sizeof(*wdev
), GFP_KERNEL
);
236 wdev
->omap_wdt_users
= false;
237 wdev
->dev
= &pdev
->dev
;
238 wdev
->wdt_trgr_pattern
= 0x1234;
239 mutex_init(&wdev
->lock
);
241 /* reserve static register mappings */
242 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
243 wdev
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
244 if (IS_ERR(wdev
->base
))
245 return PTR_ERR(wdev
->base
);
247 wdev
->wdog
.info
= &omap_wdt_info
;
248 wdev
->wdog
.ops
= &omap_wdt_ops
;
249 wdev
->wdog
.min_timeout
= TIMER_MARGIN_MIN
;
250 wdev
->wdog
.max_timeout
= TIMER_MARGIN_MAX
;
252 if (watchdog_init_timeout(&wdev
->wdog
, timer_margin
, &pdev
->dev
) < 0)
253 wdev
->wdog
.timeout
= TIMER_MARGIN_DEFAULT
;
255 watchdog_set_nowayout(&wdev
->wdog
, nowayout
);
257 platform_set_drvdata(pdev
, wdev
);
259 pm_runtime_enable(wdev
->dev
);
260 pm_runtime_get_sync(wdev
->dev
);
262 if (pdata
&& pdata
->read_reset_sources
) {
263 u32 rs
= pdata
->read_reset_sources();
264 if (rs
& (1 << OMAP_MPU_WD_RST_SRC_ID_SHIFT
))
265 wdev
->wdog
.bootstatus
= WDIOF_CARDRESET
;
268 omap_wdt_disable(wdev
);
270 ret
= watchdog_register_device(&wdev
->wdog
);
272 pm_runtime_disable(wdev
->dev
);
276 pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
277 readl_relaxed(wdev
->base
+ OMAP_WATCHDOG_REV
) & 0xFF,
280 pm_runtime_put_sync(wdev
->dev
);
285 static void omap_wdt_shutdown(struct platform_device
*pdev
)
287 struct omap_wdt_dev
*wdev
= platform_get_drvdata(pdev
);
289 mutex_lock(&wdev
->lock
);
290 if (wdev
->omap_wdt_users
) {
291 omap_wdt_disable(wdev
);
292 pm_runtime_put_sync(wdev
->dev
);
294 mutex_unlock(&wdev
->lock
);
297 static int omap_wdt_remove(struct platform_device
*pdev
)
299 struct omap_wdt_dev
*wdev
= platform_get_drvdata(pdev
);
301 pm_runtime_disable(wdev
->dev
);
302 watchdog_unregister_device(&wdev
->wdog
);
309 /* REVISIT ... not clear this is the best way to handle system suspend; and
310 * it's very inappropriate for selective device suspend (e.g. suspending this
311 * through sysfs rather than by stopping the watchdog daemon). Also, this
312 * may not play well enough with NOWAYOUT...
315 static int omap_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
317 struct omap_wdt_dev
*wdev
= platform_get_drvdata(pdev
);
319 mutex_lock(&wdev
->lock
);
320 if (wdev
->omap_wdt_users
) {
321 omap_wdt_disable(wdev
);
322 pm_runtime_put_sync(wdev
->dev
);
324 mutex_unlock(&wdev
->lock
);
329 static int omap_wdt_resume(struct platform_device
*pdev
)
331 struct omap_wdt_dev
*wdev
= platform_get_drvdata(pdev
);
333 mutex_lock(&wdev
->lock
);
334 if (wdev
->omap_wdt_users
) {
335 pm_runtime_get_sync(wdev
->dev
);
336 omap_wdt_enable(wdev
);
337 omap_wdt_reload(wdev
);
339 mutex_unlock(&wdev
->lock
);
345 #define omap_wdt_suspend NULL
346 #define omap_wdt_resume NULL
349 static const struct of_device_id omap_wdt_of_match
[] = {
350 { .compatible
= "ti,omap3-wdt", },
353 MODULE_DEVICE_TABLE(of
, omap_wdt_of_match
);
355 static struct platform_driver omap_wdt_driver
= {
356 .probe
= omap_wdt_probe
,
357 .remove
= omap_wdt_remove
,
358 .shutdown
= omap_wdt_shutdown
,
359 .suspend
= omap_wdt_suspend
,
360 .resume
= omap_wdt_resume
,
363 .of_match_table
= omap_wdt_of_match
,
367 module_platform_driver(omap_wdt_driver
);
369 MODULE_AUTHOR("George G. Davis");
370 MODULE_LICENSE("GPL");
371 MODULE_ALIAS("platform:omap_wdt");