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