at91sam9_wdt.c 10.3 KB
Newer Older
R
Renaud CERRATO 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Watchdog driver for Atmel AT91SAM9x processors.
 *
 * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

/*
 * The Watchdog Timer Mode Register can be only written to once. If the
 * timeout need to be set from Linux, be sure that the bootstrap or the
 * bootloader doesn't write to this register.
 */

18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

R
Renaud CERRATO 已提交
20 21
#include <linux/errno.h>
#include <linux/init.h>
22
#include <linux/interrupt.h>
23
#include <linux/io.h>
R
Renaud CERRATO 已提交
24 25 26 27
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
28
#include <linux/reboot.h>
R
Renaud CERRATO 已提交
29 30 31 32 33 34
#include <linux/types.h>
#include <linux/watchdog.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
#include <linux/bitops.h>
#include <linux/uaccess.h>
35
#include <linux/of.h>
36
#include <linux/of_irq.h>
R
Renaud CERRATO 已提交
37

38
#include "at91sam9_wdt.h"
R
Renaud CERRATO 已提交
39 40 41

#define DRV_NAME "AT91SAM9 Watchdog"

42 43 44 45
#define wdt_read(wdt, field) \
	__raw_readl((wdt)->base + (field))
#define wdt_write(wtd, field, val) \
	__raw_writel((val), (wdt)->base + (field))
46

R
Renaud CERRATO 已提交
47 48 49 50
/* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
 * use this to convert a watchdog
 * value from/to milliseconds.
 */
51 52 53
#define ticks_to_hz_rounddown(t)	((((t) + 1) * HZ) >> 8)
#define ticks_to_hz_roundup(t)		(((((t) + 1) * HZ) + 255) >> 8)
#define ticks_to_secs(t)		(((t) + 1) >> 8)
54
#define secs_to_ticks(s)		((s) ? (((s) << 8) - 1) : 0)
55 56 57 58 59 60 61 62

#define WDT_MR_RESET	0x3FFF2FFF

/* Watchdog max counter value in ticks */
#define WDT_COUNTER_MAX_TICKS	0xFFF

/* Watchdog max delta/value in secs */
#define WDT_COUNTER_MAX_SECS	ticks_to_secs(WDT_COUNTER_MAX_TICKS)
R
Renaud CERRATO 已提交
63 64 65 66 67 68 69 70 71

/* Hardware timeout in seconds */
#define WDT_HW_TIMEOUT 2

/* Timer heartbeat (500ms) */
#define WDT_TIMEOUT	(HZ/2)

/* User land timeout */
#define WDT_HEARTBEAT 15
72
static int heartbeat;
R
Renaud CERRATO 已提交
73 74 75 76
module_param(heartbeat, int, 0);
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
	"(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");

W
Wim Van Sebroeck 已提交
77 78
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
R
Renaud CERRATO 已提交
79 80 81
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

82 83 84
#define to_wdt(wdd) container_of(wdd, struct at91wdt, wdd)
struct at91wdt {
	struct watchdog_device wdd;
85
	void __iomem *base;
R
Renaud CERRATO 已提交
86 87
	unsigned long next_heartbeat;	/* the next_heartbeat for the timer */
	struct timer_list timer;	/* The timer that pings the watchdog */
88 89 90 91 92 93
	u32 mr;
	u32 mr_mask;
	unsigned long heartbeat;	/* WDT heartbeat in jiffies */
	bool nowayout;
	unsigned int irq;
};
R
Renaud CERRATO 已提交
94 95 96

/* ......................................................................... */

97 98 99 100 101 102 103 104 105 106 107 108 109
static irqreturn_t wdt_interrupt(int irq, void *dev_id)
{
	struct at91wdt *wdt = (struct at91wdt *)dev_id;

	if (wdt_read(wdt, AT91_WDT_SR)) {
		pr_crit("at91sam9 WDT software reset\n");
		emergency_restart();
		pr_crit("Reboot didn't ?????\n");
	}

	return IRQ_HANDLED;
}

R
Renaud CERRATO 已提交
110 111 112
/*
 * Reload the watchdog timer.  (ie, pat the watchdog)
 */
113
static inline void at91_wdt_reset(struct at91wdt *wdt)
R
Renaud CERRATO 已提交
114
{
115
	wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
R
Renaud CERRATO 已提交
116 117 118 119 120 121 122
}

/*
 * Timer tick
 */
static void at91_ping(unsigned long data)
{
123 124 125 126 127 128
	struct at91wdt *wdt = (struct at91wdt *)data;
	if (time_before(jiffies, wdt->next_heartbeat) ||
	    !watchdog_active(&wdt->wdd)) {
		at91_wdt_reset(wdt);
		mod_timer(&wdt->timer, jiffies + wdt->heartbeat);
	} else {
129
		pr_crit("I will reset your machine !\n");
130
	}
131
}
R
Renaud CERRATO 已提交
132

133 134
static int at91_wdt_start(struct watchdog_device *wdd)
{
135 136 137
	struct at91wdt *wdt = to_wdt(wdd);
	/* calculate when the next userspace timeout will be */
	wdt->next_heartbeat = jiffies + wdd->timeout * HZ;
138
	return 0;
R
Renaud CERRATO 已提交
139 140
}

141
static int at91_wdt_stop(struct watchdog_device *wdd)
R
Renaud CERRATO 已提交
142
{
143 144 145
	/* The watchdog timer hardware can not be stopped... */
	return 0;
}
R
Renaud CERRATO 已提交
146

147 148 149
static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout)
{
	wdd->timeout = new_timeout;
150
	return at91_wdt_start(wdd);
R
Renaud CERRATO 已提交
151 152
}

153
static int at91_wdt_init(struct platform_device *pdev, struct at91wdt *wdt)
R
Renaud CERRATO 已提交
154
{
155 156 157 158 159 160
	u32 tmp;
	u32 delta;
	u32 value;
	int err;
	u32 mask = wdt->mr_mask;
	unsigned long min_heartbeat = 1;
161
	unsigned long max_heartbeat;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	struct device *dev = &pdev->dev;

	tmp = wdt_read(wdt, AT91_WDT_MR);
	if ((tmp & mask) != (wdt->mr & mask)) {
		if (tmp == WDT_MR_RESET) {
			wdt_write(wdt, AT91_WDT_MR, wdt->mr);
			tmp = wdt_read(wdt, AT91_WDT_MR);
		}
	}

	if (tmp & AT91_WDT_WDDIS) {
		if (wdt->mr & AT91_WDT_WDDIS)
			return 0;
		dev_err(dev, "watchdog is disabled\n");
		return -EINVAL;
	}

	value = tmp & AT91_WDT_WDV;
	delta = (tmp & AT91_WDT_WDD) >> 16;

	if (delta < value)
		min_heartbeat = ticks_to_hz_roundup(value - delta);

185 186
	max_heartbeat = ticks_to_hz_rounddown(value);
	if (!max_heartbeat) {
187 188 189 190 191
		dev_err(dev,
			"heartbeat is too small for the system to handle it correctly\n");
		return -EINVAL;
	}

192 193 194 195 196 197 198 199 200 201 202
	/*
	 * Try to reset the watchdog counter 4 or 2 times more often than
	 * actually requested, to avoid spurious watchdog reset.
	 * If this is not possible because of the min_heartbeat value, reset
	 * it at the min_heartbeat period.
	 */
	if ((max_heartbeat / 4) >= min_heartbeat)
		wdt->heartbeat = max_heartbeat / 4;
	else if ((max_heartbeat / 2) >= min_heartbeat)
		wdt->heartbeat = max_heartbeat / 2;
	else
203
		wdt->heartbeat = min_heartbeat;
204 205

	if (max_heartbeat < min_heartbeat + 4)
206 207
		dev_warn(dev,
			 "min heartbeat and max heartbeat might be too close for the system to handle it correctly\n");
R
Renaud CERRATO 已提交
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222
	if ((tmp & AT91_WDT_WDFIEN) && wdt->irq) {
		err = request_irq(wdt->irq, wdt_interrupt,
				  IRQF_SHARED | IRQF_IRQPOLL,
				  pdev->name, wdt);
		if (err)
			return err;
	}

	if ((tmp & wdt->mr_mask) != (wdt->mr & wdt->mr_mask))
		dev_warn(dev,
			 "watchdog already configured differently (mr = %x expecting %x)\n",
			 tmp & wdt->mr_mask, wdt->mr & wdt->mr_mask);

	setup_timer(&wdt->timer, at91_ping, (unsigned long)wdt);
223 224 225 226 227 228 229 230 231

	/*
	 * Use min_heartbeat the first time to avoid spurious watchdog reset:
	 * we don't know for how long the watchdog counter is running, and
	 *  - resetting it right now might trigger a watchdog fault reset
	 *  - waiting for heartbeat time might lead to a watchdog timeout
	 *    reset
	 */
	mod_timer(&wdt->timer, jiffies + min_heartbeat);
232 233 234 235 236 237 238 239 240 241

	/* Try to set timeout from device tree first */
	if (watchdog_init_timeout(&wdt->wdd, 0, dev))
		watchdog_init_timeout(&wdt->wdd, heartbeat, dev);
	watchdog_set_nowayout(&wdt->wdd, wdt->nowayout);
	err = watchdog_register_device(&wdt->wdd);
	if (err)
		goto out_stop_timer;

	wdt->next_heartbeat = jiffies + wdt->wdd.timeout * HZ;
R
Renaud CERRATO 已提交
242 243

	return 0;
244 245 246 247

out_stop_timer:
	del_timer(&wdt->timer);
	return err;
R
Renaud CERRATO 已提交
248 249
}

250 251
/* ......................................................................... */

R
Renaud CERRATO 已提交
252 253
static const struct watchdog_info at91_wdt_info = {
	.identity	= DRV_NAME,
254 255
	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
						WDIOF_MAGICCLOSE,
R
Renaud CERRATO 已提交
256 257
};

258 259 260 261 262
static const struct watchdog_ops at91_wdt_ops = {
	.owner =	THIS_MODULE,
	.start =	at91_wdt_start,
	.stop =		at91_wdt_stop,
	.set_timeout =	at91_wdt_set_timeout,
R
Renaud CERRATO 已提交
263 264
};

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
#if defined(CONFIG_OF)
static int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
{
	u32 min = 0;
	u32 max = WDT_COUNTER_MAX_SECS;
	const char *tmp;

	/* Get the interrupts property */
	wdt->irq = irq_of_parse_and_map(np, 0);
	if (!wdt->irq)
		dev_warn(wdt->wdd.parent, "failed to get IRQ from DT\n");

	if (!of_property_read_u32_index(np, "atmel,max-heartbeat-sec", 0,
					&max)) {
		if (!max || max > WDT_COUNTER_MAX_SECS)
			max = WDT_COUNTER_MAX_SECS;

		if (!of_property_read_u32_index(np, "atmel,min-heartbeat-sec",
						0, &min)) {
			if (min >= max)
				min = max - 1;
		}
	}

	min = secs_to_ticks(min);
	max = secs_to_ticks(max);

	wdt->mr_mask = 0x3FFFFFFF;
	wdt->mr = 0;
	if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
	    !strcmp(tmp, "software")) {
		wdt->mr |= AT91_WDT_WDFIEN;
		wdt->mr_mask &= ~AT91_WDT_WDRPROC;
	} else {
		wdt->mr |= AT91_WDT_WDRSTEN;
	}

	if (!of_property_read_string(np, "atmel,reset-type", &tmp) &&
	    !strcmp(tmp, "proc"))
		wdt->mr |= AT91_WDT_WDRPROC;

	if (of_property_read_bool(np, "atmel,disable")) {
		wdt->mr |= AT91_WDT_WDDIS;
		wdt->mr_mask &= AT91_WDT_WDDIS;
	}

	if (of_property_read_bool(np, "atmel,idle-halt"))
		wdt->mr |= AT91_WDT_WDIDLEHLT;

	if (of_property_read_bool(np, "atmel,dbg-halt"))
		wdt->mr |= AT91_WDT_WDDBGHLT;

	wdt->mr |= max | ((max - min) << 16);

	return 0;
}
#else
static inline int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
{
	return 0;
}
#endif
R
Renaud CERRATO 已提交
327 328 329

static int __init at91wdt_probe(struct platform_device *pdev)
{
330
	struct resource	*r;
331 332
	int err;
	struct at91wdt *wdt;
R
Renaud CERRATO 已提交
333

334 335
	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
	if (!wdt)
336 337
		return -ENOMEM;

338 339 340 341 342 343 344 345 346 347
	wdt->mr = (WDT_HW_TIMEOUT * 256) | AT91_WDT_WDRSTEN | AT91_WDT_WDD |
		  AT91_WDT_WDDBGHLT | AT91_WDT_WDIDLEHLT;
	wdt->mr_mask = 0x3FFFFFFF;
	wdt->nowayout = nowayout;
	wdt->wdd.parent = &pdev->dev;
	wdt->wdd.info = &at91_wdt_info;
	wdt->wdd.ops = &at91_wdt_ops;
	wdt->wdd.timeout = WDT_HEARTBEAT;
	wdt->wdd.min_timeout = 1;
	wdt->wdd.max_timeout = 0xFFFF;
348

349 350 351 352 353 354 355 356 357 358
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	wdt->base = devm_ioremap_resource(&pdev->dev, r);
	if (IS_ERR(wdt->base))
		return PTR_ERR(wdt->base);

	if (pdev->dev.of_node) {
		err = of_at91wdt_init(pdev->dev.of_node, wdt);
		if (err)
			return err;
	}
R
Renaud CERRATO 已提交
359

360 361 362
	err = at91_wdt_init(pdev, wdt);
	if (err)
		return err;
R
Renaud CERRATO 已提交
363

364
	platform_set_drvdata(pdev, wdt);
R
Renaud CERRATO 已提交
365

366
	pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
367
		wdt->wdd.timeout, wdt->nowayout);
R
Renaud CERRATO 已提交
368 369 370 371 372 373

	return 0;
}

static int __exit at91wdt_remove(struct platform_device *pdev)
{
374 375
	struct at91wdt *wdt = platform_get_drvdata(pdev);
	watchdog_unregister_device(&wdt->wdd);
R
Renaud CERRATO 已提交
376

377
	pr_warn("I quit now, hardware will probably reboot!\n");
378
	del_timer(&wdt->timer);
R
Renaud CERRATO 已提交
379

380
	return 0;
R
Renaud CERRATO 已提交
381 382
}

383
#if defined(CONFIG_OF)
384
static const struct of_device_id at91_wdt_dt_ids[] = {
385 386 387 388 389 390 391
	{ .compatible = "atmel,at91sam9260-wdt" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
#endif

R
Renaud CERRATO 已提交
392 393 394 395 396
static struct platform_driver at91wdt_driver = {
	.remove		= __exit_p(at91wdt_remove),
	.driver		= {
		.name	= "at91_wdt",
		.owner	= THIS_MODULE,
397
		.of_match_table = of_match_ptr(at91_wdt_dt_ids),
R
Renaud CERRATO 已提交
398 399 400
	},
};

401
module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
R
Renaud CERRATO 已提交
402 403 404 405

MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
MODULE_LICENSE("GPL");