omap_wdt.c 9.4 KB
Newer Older
1
/*
2
 * omap_wdt.c
3
 *
4
 * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Author: MontaVista Software, Inc.
 *	 <gdavis@mvista.com> or <source@mvista.com>
 *
 * 2003 (c) MontaVista Software, Inc. This file is licensed under the
 * terms of the GNU General Public License version 2. This program is
 * licensed "as is" without any warranty of any kind, whether express
 * or implied.
 *
 * History:
 *
 * 20030527: George G. Davis <gdavis@mvista.com>
 *	Initially based on linux-2.4.19-rmk7-pxa1/drivers/char/sa1100_wdt.c
 *	(c) Copyright 2000 Oleg Drokin <green@crimea.edu>
19
 *	Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk>
20 21 22 23 24 25 26 27 28
 *
 * Copyright (c) 2004 Texas Instruments.
 *	1. Modified to support OMAP1610 32-KHz watchdog timer
 *	2. Ported to 2.6 kernel
 *
 * Copyright (c) 2005 David Brownell
 *	Use the driver model and standard identifiers; handle bigger timeouts.
 */

29 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

31 32 33 34 35 36 37 38 39 40
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/watchdog.h>
#include <linux/reboot.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/moduleparam.h>
W
Wim Van Sebroeck 已提交
41
#include <linux/io.h>
42
#include <linux/slab.h>
43
#include <linux/pm_runtime.h>
44
#include <linux/platform_data/omap-wd-timer.h>
45 46 47

#include "omap_wdt.h"

48 49 50 51 52
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

53 54 55 56
static unsigned timer_margin;
module_param(timer_margin, uint, 0);
MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)");

57 58 59
struct omap_wdt_dev {
	void __iomem    *base;          /* physical */
	struct device   *dev;
60
	bool		omap_wdt_users;
61
	struct resource *mem;
62 63
	int		wdt_trgr_pattern;
	struct mutex	lock;		/* to avoid races with PM */
64 65
};

66
static void omap_wdt_reload(struct omap_wdt_dev *wdev)
67
{
68
	void __iomem    *base = wdev->base;
69

70
	/* wait for posted write to complete */
71
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
72
		cpu_relax();
73

74 75
	wdev->wdt_trgr_pattern = ~wdev->wdt_trgr_pattern;
	__raw_writel(wdev->wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
76

77
	/* wait for posted write to complete */
78
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
79 80 81 82
		cpu_relax();
	/* reloaded WCRR from WLDR */
}

83
static void omap_wdt_enable(struct omap_wdt_dev *wdev)
84
{
85 86
	void __iomem *base = wdev->base;

87
	/* Sequence to enable the watchdog */
88 89
	__raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
90
		cpu_relax();
91

92 93
	__raw_writel(0x4444, base + OMAP_WATCHDOG_SPR);
	while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
94 95 96
		cpu_relax();
}

97
static void omap_wdt_disable(struct omap_wdt_dev *wdev)
98
{
99 100
	void __iomem *base = wdev->base;

101
	/* sequence required to disable watchdog */
102 103
	__raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR);	/* TIMER_MODE */
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
104
		cpu_relax();
105

106 107
	__raw_writel(0x5555, base + OMAP_WATCHDOG_SPR);	/* TIMER_MODE */
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
108 109 110
		cpu_relax();
}

111 112
static void omap_wdt_set_timer(struct omap_wdt_dev *wdev,
				   unsigned int timeout)
113
{
114
	u32 pre_margin = GET_WLDR_VAL(timeout);
115
	void __iomem *base = wdev->base;
116 117

	/* just count up at 32 KHz */
118
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
119
		cpu_relax();
120

121 122
	__raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
123 124 125
		cpu_relax();
}

126
static int omap_wdt_start(struct watchdog_device *wdog)
127
{
128
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
129 130
	void __iomem *base = wdev->base;

131 132 133
	mutex_lock(&wdev->lock);

	wdev->omap_wdt_users = true;
134

135
	pm_runtime_get_sync(wdev->dev);
136 137

	/* initialize prescaler */
138
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
139
		cpu_relax();
140

141 142
	__raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
	while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
143 144
		cpu_relax();

145 146
	omap_wdt_set_timer(wdev, wdog->timeout);
	omap_wdt_reload(wdev); /* trigger loading of new timeout value */
147
	omap_wdt_enable(wdev);
148

149 150 151
	mutex_unlock(&wdev->lock);

	return 0;
152 153
}

154
static int omap_wdt_stop(struct watchdog_device *wdog)
155
{
156
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
157

158
	mutex_lock(&wdev->lock);
159
	omap_wdt_disable(wdev);
160
	pm_runtime_put_sync(wdev->dev);
161 162
	wdev->omap_wdt_users = false;
	mutex_unlock(&wdev->lock);
163 164 165
	return 0;
}

166
static int omap_wdt_ping(struct watchdog_device *wdog)
167
{
168
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
169

170 171 172 173 174
	mutex_lock(&wdev->lock);
	omap_wdt_reload(wdev);
	mutex_unlock(&wdev->lock);

	return 0;
175 176
}

177 178
static int omap_wdt_set_timeout(struct watchdog_device *wdog,
				unsigned int timeout)
179
{
180
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
181

182 183 184 185 186 187 188 189 190
	mutex_lock(&wdev->lock);
	omap_wdt_disable(wdev);
	omap_wdt_set_timer(wdev, timeout);
	omap_wdt_enable(wdev);
	omap_wdt_reload(wdev);
	wdog->timeout = timeout;
	mutex_unlock(&wdev->lock);

	return 0;
191 192
}

193 194 195 196 197 198 199 200 201 202 203
static const struct watchdog_info omap_wdt_info = {
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
	.identity = "OMAP Watchdog",
};

static const struct watchdog_ops omap_wdt_ops = {
	.owner		= THIS_MODULE,
	.start		= omap_wdt_start,
	.stop		= omap_wdt_stop,
	.ping		= omap_wdt_ping,
	.set_timeout	= omap_wdt_set_timeout,
204 205
};

B
Bill Pemberton 已提交
206
static int omap_wdt_probe(struct platform_device *pdev)
207
{
208 209
	struct omap_wd_timer_platform_data *pdata = pdev->dev.platform_data;
	struct watchdog_device *omap_wdt;
210
	struct resource *res, *mem;
211
	struct omap_wdt_dev *wdev;
212
	u32 rs;
213
	int ret;
214

215
	omap_wdt = devm_kzalloc(&pdev->dev, sizeof(*omap_wdt), GFP_KERNEL);
216 217 218
	if (!omap_wdt)
		return -ENOMEM;

219 220
	/* reserve static register mappings */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
221 222
	if (!res)
		return -ENOENT;
223

224 225 226 227
	mem = devm_request_mem_region(&pdev->dev, res->start,
				      resource_size(res), pdev->name);
	if (!mem)
		return -EBUSY;
228

229 230 231
	wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
	if (!wdev)
		return -ENOMEM;
232

233 234 235 236 237
	wdev->omap_wdt_users	= false;
	wdev->mem		= mem;
	wdev->dev		= &pdev->dev;
	wdev->wdt_trgr_pattern	= 0x1234;
	mutex_init(&wdev->lock);
238

239 240 241
	wdev->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (!wdev->base)
		return -ENOMEM;
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	omap_wdt->info	      = &omap_wdt_info;
	omap_wdt->ops	      = &omap_wdt_ops;
	omap_wdt->min_timeout = TIMER_MARGIN_MIN;
	omap_wdt->max_timeout = TIMER_MARGIN_MAX;

	if (timer_margin >= TIMER_MARGIN_MIN &&
	    timer_margin <= TIMER_MARGIN_MAX)
		omap_wdt->timeout = timer_margin;
	else
		omap_wdt->timeout = TIMER_MARGIN_DEFAULT;

	watchdog_set_drvdata(omap_wdt, wdev);
	watchdog_set_nowayout(omap_wdt, nowayout);

	platform_set_drvdata(pdev, omap_wdt);
258

259 260
	pm_runtime_enable(wdev->dev);
	pm_runtime_get_sync(wdev->dev);
261

262 263 264 265 266 267
	if (pdata && pdata->read_reset_sources)
		rs = pdata->read_reset_sources();
	else
		rs = 0;
	omap_wdt->bootstatus = (rs & (1 << OMAP_MPU_WD_RST_SRC_ID_SHIFT)) ?
				WDIOF_CARDRESET : 0;
268

269
	omap_wdt_disable(wdev);
270

271
	ret = watchdog_register_device(omap_wdt);
A
Aaro Koskinen 已提交
272 273 274 275
	if (ret) {
		pm_runtime_disable(wdev->dev);
		return ret;
	}
276

277
	pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
278
		__raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
279
		omap_wdt->timeout);
280

281
	pm_runtime_put_sync(wdev->dev);
282

283 284 285 286 287
	return 0;
}

static void omap_wdt_shutdown(struct platform_device *pdev)
{
288 289
	struct watchdog_device *wdog = platform_get_drvdata(pdev);
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
290

291
	mutex_lock(&wdev->lock);
292
	if (wdev->omap_wdt_users) {
293
		omap_wdt_disable(wdev);
294 295
		pm_runtime_put_sync(wdev->dev);
	}
296
	mutex_unlock(&wdev->lock);
297 298
}

B
Bill Pemberton 已提交
299
static int omap_wdt_remove(struct platform_device *pdev)
300
{
301 302
	struct watchdog_device *wdog = platform_get_drvdata(pdev);
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
303

304
	pm_runtime_disable(wdev->dev);
305
	watchdog_unregister_device(wdog);
306

307 308 309 310 311 312 313 314 315 316 317 318 319
	return 0;
}

#ifdef	CONFIG_PM

/* REVISIT ... not clear this is the best way to handle system suspend; and
 * it's very inappropriate for selective device suspend (e.g. suspending this
 * through sysfs rather than by stopping the watchdog daemon).  Also, this
 * may not play well enough with NOWAYOUT...
 */

static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
{
320 321
	struct watchdog_device *wdog = platform_get_drvdata(pdev);
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
322

323
	mutex_lock(&wdev->lock);
324
	if (wdev->omap_wdt_users) {
325
		omap_wdt_disable(wdev);
326 327
		pm_runtime_put_sync(wdev->dev);
	}
328
	mutex_unlock(&wdev->lock);
329

330 331 332 333 334
	return 0;
}

static int omap_wdt_resume(struct platform_device *pdev)
{
335 336
	struct watchdog_device *wdog = platform_get_drvdata(pdev);
	struct omap_wdt_dev *wdev = watchdog_get_drvdata(wdog);
337

338
	mutex_lock(&wdev->lock);
339
	if (wdev->omap_wdt_users) {
340
		pm_runtime_get_sync(wdev->dev);
341
		omap_wdt_enable(wdev);
342
		omap_wdt_reload(wdev);
343
	}
344
	mutex_unlock(&wdev->lock);
345

346 347 348 349 350 351 352 353
	return 0;
}

#else
#define	omap_wdt_suspend	NULL
#define	omap_wdt_resume		NULL
#endif

354 355 356 357 358 359
static const struct of_device_id omap_wdt_of_match[] = {
	{ .compatible = "ti,omap3-wdt", },
	{},
};
MODULE_DEVICE_TABLE(of, omap_wdt_of_match);

360 361
static struct platform_driver omap_wdt_driver = {
	.probe		= omap_wdt_probe,
362
	.remove		= omap_wdt_remove,
363 364 365 366 367 368
	.shutdown	= omap_wdt_shutdown,
	.suspend	= omap_wdt_suspend,
	.resume		= omap_wdt_resume,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "omap_wdt",
369
		.of_match_table = omap_wdt_of_match,
370 371 372
	},
};

373
module_platform_driver(omap_wdt_driver);
374 375 376

MODULE_AUTHOR("George G. Davis");
MODULE_LICENSE("GPL");
377
MODULE_ALIAS("platform:omap_wdt");