at91sam9_wdt.c 6.4 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/io.h>
R
Renaud CERRATO 已提交
23 24 25 26 27 28 29 30 31 32
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/watchdog.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
#include <linux/bitops.h>
#include <linux/uaccess.h>
33
#include <linux/of.h>
R
Renaud CERRATO 已提交
34

35
#include "at91sam9_wdt.h"
R
Renaud CERRATO 已提交
36 37 38

#define DRV_NAME "AT91SAM9 Watchdog"

39 40 41 42 43
#define wdt_read(field) \
	__raw_readl(at91wdt_private.base + field)
#define wdt_write(field, val) \
	__raw_writel((val), at91wdt_private.base + field)

R
Renaud CERRATO 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
 * use this to convert a watchdog
 * value from/to milliseconds.
 */
#define ms_to_ticks(t)	(((t << 8) / 1000) - 1)
#define ticks_to_ms(t)	(((t + 1) * 1000) >> 8)

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

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

/* User land timeout */
#define WDT_HEARTBEAT 15
static int heartbeat = WDT_HEARTBEAT;
module_param(heartbeat, int, 0);
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
	"(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");

W
Wim Van Sebroeck 已提交
64 65
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
R
Renaud CERRATO 已提交
66 67 68
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
	"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

69
static struct watchdog_device at91_wdt_dev;
R
Renaud CERRATO 已提交
70 71 72
static void at91_ping(unsigned long data);

static struct {
73
	void __iomem *base;
R
Renaud CERRATO 已提交
74 75 76 77 78 79 80 81 82 83 84
	unsigned long next_heartbeat;	/* the next_heartbeat for the timer */
	struct timer_list timer;	/* The timer that pings the watchdog */
} at91wdt_private;

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

/*
 * Reload the watchdog timer.  (ie, pat the watchdog)
 */
static inline void at91_wdt_reset(void)
{
85
	wdt_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
R
Renaud CERRATO 已提交
86 87 88 89 90 91 92 93
}

/*
 * Timer tick
 */
static void at91_ping(unsigned long data)
{
	if (time_before(jiffies, at91wdt_private.next_heartbeat) ||
94
	    (!watchdog_active(&at91_wdt_dev))) {
R
Renaud CERRATO 已提交
95 96 97
		at91_wdt_reset();
		mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
	} else
98
		pr_crit("I will reset your machine !\n");
R
Renaud CERRATO 已提交
99 100
}

101
static int at91_wdt_ping(struct watchdog_device *wdd)
R
Renaud CERRATO 已提交
102
{
103 104 105 106
	/* calculate when the next userspace timeout will be */
	at91wdt_private.next_heartbeat = jiffies + wdd->timeout * HZ;
	return 0;
}
R
Renaud CERRATO 已提交
107

108 109 110 111
static int at91_wdt_start(struct watchdog_device *wdd)
{
	/* calculate the next userspace timeout and modify the timer */
	at91_wdt_ping(wdd);
R
Renaud CERRATO 已提交
112
	mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
113
	return 0;
R
Renaud CERRATO 已提交
114 115
}

116
static int at91_wdt_stop(struct watchdog_device *wdd)
R
Renaud CERRATO 已提交
117
{
118 119 120
	/* The watchdog timer hardware can not be stopped... */
	return 0;
}
R
Renaud CERRATO 已提交
121

122 123 124
static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout)
{
	wdd->timeout = new_timeout;
R
Renaud CERRATO 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137
	return 0;
}

/*
 * Set the watchdog time interval in 1/256Hz (write-once)
 * Counter is 12 bit.
 */
static int at91_wdt_settimeout(unsigned int timeout)
{
	unsigned int reg;
	unsigned int mr;

	/* Check if disabled */
138
	mr = wdt_read(AT91_WDT_MR);
R
Renaud CERRATO 已提交
139
	if (mr & AT91_WDT_WDDIS) {
140
		pr_err("sorry, watchdog is disabled\n");
R
Renaud CERRATO 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154
		return -EIO;
	}

	/*
	 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
	 *
	 * Since WDV is a 12-bit counter, the maximum period is
	 * 4096 / 256 = 16 seconds.
	 */
	reg = AT91_WDT_WDRSTEN	/* causes watchdog reset */
		/* | AT91_WDT_WDRPROC	causes processor reset only */
		| AT91_WDT_WDDBGHLT	/* disabled in debug mode */
		| AT91_WDT_WDD		/* restart at any time */
		| (timeout & AT91_WDT_WDV);  /* timer value */
155
	wdt_write(AT91_WDT_MR, reg);
R
Renaud CERRATO 已提交
156 157 158 159

	return 0;
}

160 161
/* ......................................................................... */

R
Renaud CERRATO 已提交
162 163
static const struct watchdog_info at91_wdt_info = {
	.identity	= DRV_NAME,
164 165
	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
						WDIOF_MAGICCLOSE,
R
Renaud CERRATO 已提交
166 167
};

168 169 170 171 172 173
static const struct watchdog_ops at91_wdt_ops = {
	.owner =	THIS_MODULE,
	.start =	at91_wdt_start,
	.stop =		at91_wdt_stop,
	.ping =		at91_wdt_ping,
	.set_timeout =	at91_wdt_set_timeout,
R
Renaud CERRATO 已提交
174 175
};

176 177 178 179 180
static struct watchdog_device at91_wdt_dev = {
	.info =		&at91_wdt_info,
	.ops =		&at91_wdt_ops,
	.min_timeout =	1,
	.max_timeout =	0xFFFF,
R
Renaud CERRATO 已提交
181 182 183 184
};

static int __init at91wdt_probe(struct platform_device *pdev)
{
185
	struct resource	*r;
R
Renaud CERRATO 已提交
186 187
	int res;

188 189 190 191 192 193 194 195 196
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r)
		return -ENODEV;
	at91wdt_private.base = ioremap(r->start, resource_size(r));
	if (!at91wdt_private.base) {
		dev_err(&pdev->dev, "failed to map registers, aborting.\n");
		return -ENOMEM;
	}

197 198 199 200
	at91_wdt_dev.timeout = heartbeat;
	at91_wdt_dev.parent = &pdev->dev;
	watchdog_set_nowayout(&at91_wdt_dev, nowayout);

R
Renaud CERRATO 已提交
201 202 203 204 205
	/* Set watchdog */
	res = at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));
	if (res)
		return res;

206
	res = watchdog_register_device(&at91_wdt_dev);
R
Renaud CERRATO 已提交
207 208 209
	if (res)
		return res;

210
	at91wdt_private.next_heartbeat = jiffies + at91_wdt_dev.timeout * HZ;
R
Renaud CERRATO 已提交
211 212 213
	setup_timer(&at91wdt_private.timer, at91_ping, 0);
	mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);

214
	pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
R
Renaud CERRATO 已提交
215 216 217 218 219 220 221
		heartbeat, nowayout);

	return 0;
}

static int __exit at91wdt_remove(struct platform_device *pdev)
{
222
	watchdog_unregister_device(&at91_wdt_dev);
R
Renaud CERRATO 已提交
223

224 225
	pr_warn("I quit now, hardware will probably reboot!\n");
	del_timer(&at91wdt_private.timer);
R
Renaud CERRATO 已提交
226

227
	return 0;
R
Renaud CERRATO 已提交
228 229
}

230
#if defined(CONFIG_OF)
231
static const struct of_device_id at91_wdt_dt_ids[] = {
232 233 234 235 236 237 238
	{ .compatible = "atmel,at91sam9260-wdt" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
#endif

R
Renaud CERRATO 已提交
239 240 241 242 243
static struct platform_driver at91wdt_driver = {
	.remove		= __exit_p(at91wdt_remove),
	.driver		= {
		.name	= "at91_wdt",
		.owner	= THIS_MODULE,
244
		.of_match_table = of_match_ptr(at91_wdt_dt_ids),
R
Renaud CERRATO 已提交
245 246 247
	},
};

248
module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
R
Renaud CERRATO 已提交
249 250 251 252

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