orion_wdt.c 5.9 KB
Newer Older
1
/*
2
 * drivers/watchdog/orion_wdt.c
3
 *
4
 * Watchdog driver for Orion/Kirkwood processors
5 6 7 8 9 10 11 12
 *
 * Author: Sylver Bruneau <sylver.bruneau@googlemail.com>
 *
 * 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.
 */

13 14
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

15 16 17 18
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/kernel.h>
19
#include <linux/platform_device.h>
20 21
#include <linux/watchdog.h>
#include <linux/init.h>
22
#include <linux/interrupt.h>
23
#include <linux/io.h>
24
#include <linux/clk.h>
25
#include <linux/err.h>
26
#include <linux/of.h>
27
#include <mach/bridge-regs.h>
28 29 30 31

/*
 * Watchdog timer block registers.
 */
32
#define TIMER_CTRL		0x0000
33
#define WDT_EN			0x0010
34
#define WDT_VAL			0x0024
35

36
#define WDT_MAX_CYCLE_COUNT	0xffffffff
37

38 39 40
#define WDT_RESET_OUT_EN	BIT(1)
#define WDT_INT_REQ		BIT(3)

W
Wim Van Sebroeck 已提交
41
static bool nowayout = WATCHDOG_NOWAYOUT;
42 43
static int heartbeat = -1;		/* module parameter (seconds) */
static unsigned int wdt_max_duration;	/* (seconds) */
44
static struct clk *clk;
45
static unsigned int wdt_tclk;
46
static void __iomem *wdt_reg;
47

48
static int orion_wdt_ping(struct watchdog_device *wdt_dev)
49 50
{
	/* Reload watchdog duration */
51 52
	writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
	return 0;
53 54
}

55
static int orion_wdt_start(struct watchdog_device *wdt_dev)
56 57
{
	/* Set watchdog duration */
58
	writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
59 60

	/* Clear watchdog timer interrupt */
61
	writel(~WDT_INT_REQ, BRIDGE_CAUSE);
62 63

	/* Enable watchdog timer */
64
	atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, WDT_EN);
65 66

	/* Enable reset on watchdog */
67
	atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, WDT_RESET_OUT_EN);
68
	return 0;
69 70
}

71
static int orion_wdt_stop(struct watchdog_device *wdt_dev)
72 73
{
	/* Disable reset on watchdog */
74
	atomic_io_modify(RSTOUTn_MASK, WDT_RESET_OUT_EN, 0);
75 76

	/* Disable watchdog timer */
77
	atomic_io_modify(wdt_reg + TIMER_CTRL, WDT_EN, 0);
78
	return 0;
79 80
}

81 82 83 84 85 86 87 88 89 90
static int orion_wdt_enabled(void)
{
	bool enabled, running;

	enabled = readl(RSTOUTn_MASK) & WDT_RESET_OUT_EN;
	running = readl(wdt_reg + TIMER_CTRL) & WDT_EN;

	return enabled && running;
}

91
static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
92
{
93
	return readl(wdt_reg + WDT_VAL) / wdt_tclk;
94 95
}

96 97
static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
				 unsigned int timeout)
98
{
99
	wdt_dev->timeout = timeout;
100 101 102
	return 0;
}

103 104 105
static const struct watchdog_info orion_wdt_info = {
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
	.identity = "Orion Watchdog",
106 107
};

108 109 110 111 112 113 114
static const struct watchdog_ops orion_wdt_ops = {
	.owner = THIS_MODULE,
	.start = orion_wdt_start,
	.stop = orion_wdt_stop,
	.ping = orion_wdt_ping,
	.set_timeout = orion_wdt_set_timeout,
	.get_timeleft = orion_wdt_get_timeleft,
115 116
};

117 118 119
static struct watchdog_device orion_wdt = {
	.info = &orion_wdt_info,
	.ops = &orion_wdt_ops,
120
	.min_timeout = 1,
121 122
};

123 124 125 126 127 128
static irqreturn_t orion_wdt_irq(int irq, void *devid)
{
	panic("Watchdog Timeout");
	return IRQ_HANDLED;
}

B
Bill Pemberton 已提交
129
static int orion_wdt_probe(struct platform_device *pdev)
130
{
131
	struct resource *res;
132
	int ret, irq;
133

134
	clk = devm_clk_get(&pdev->dev, NULL);
135
	if (IS_ERR(clk)) {
136
		dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
137
		return PTR_ERR(clk);
138
	}
139 140 141
	ret = clk_prepare_enable(clk);
	if (ret)
		return ret;
142
	wdt_tclk = clk_get_rate(clk);
143

144
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 146 147 148 149
	if (!res) {
		ret = -ENODEV;
		goto disable_clk;
	}

150
	wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
151 152 153 154
	if (!wdt_reg) {
		ret = -ENOMEM;
		goto disable_clk;
	}
155 156

	wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
157

158
	orion_wdt.timeout = wdt_max_duration;
159
	orion_wdt.max_timeout = wdt_max_duration;
160
	watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
161

162 163 164 165 166 167 168 169 170
	/*
	 * Let's make sure the watchdog is fully stopped, unless it's
	 * explicitly enabled. This may be the case if the module was
	 * removed and re-insterted, or if the bootloader explicitly
	 * set a running watchdog before booting the kernel.
	 */
	if (!orion_wdt_enabled())
		orion_wdt_stop(&orion_wdt);

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	/* Request the IRQ only after the watchdog is disabled */
	irq = platform_get_irq(pdev, 0);
	if (irq > 0) {
		/*
		 * Not all supported platforms specify an interrupt for the
		 * watchdog, so let's make it optional.
		 */
		ret = devm_request_irq(&pdev->dev, irq, orion_wdt_irq, 0,
				       pdev->name, &orion_wdt);
		if (ret < 0) {
			dev_err(&pdev->dev, "failed to request IRQ\n");
			goto disable_clk;
		}
	}

186 187
	watchdog_set_nowayout(&orion_wdt, nowayout);
	ret = watchdog_register_device(&orion_wdt);
188 189
	if (ret)
		goto disable_clk;
190

191
	pr_info("Initial timeout %d sec%s\n",
192
		orion_wdt.timeout, nowayout ? ", nowayout" : "");
193
	return 0;
194 195 196 197

disable_clk:
	clk_disable_unprepare(clk);
	return ret;
198 199
}

B
Bill Pemberton 已提交
200
static int orion_wdt_remove(struct platform_device *pdev)
201
{
202
	watchdog_unregister_device(&orion_wdt);
203
	clk_disable_unprepare(clk);
204
	return 0;
205 206
}

207
static void orion_wdt_shutdown(struct platform_device *pdev)
208
{
209
	orion_wdt_stop(&orion_wdt);
210 211
}

212
static const struct of_device_id orion_wdt_of_match_table[] = {
213 214 215 216 217
	{ .compatible = "marvell,orion-wdt", },
	{},
};
MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);

218 219
static struct platform_driver orion_wdt_driver = {
	.probe		= orion_wdt_probe,
220
	.remove		= orion_wdt_remove,
221
	.shutdown	= orion_wdt_shutdown,
222 223
	.driver		= {
		.owner	= THIS_MODULE,
224
		.name	= "orion_wdt",
225
		.of_match_table = orion_wdt_of_match_table,
226 227 228
	},
};

229
module_platform_driver(orion_wdt_driver);
230 231

MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
232
MODULE_DESCRIPTION("Orion Processor Watchdog");
233 234

module_param(heartbeat, int, 0);
235
MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
236

W
Wim Van Sebroeck 已提交
237
module_param(nowayout, bool, 0);
238 239
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
240 241

MODULE_LICENSE("GPL");
242
MODULE_ALIAS("platform:orion_wdt");