orion_wdt.c 5.3 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 22
#include <linux/watchdog.h>
#include <linux/init.h>
#include <linux/io.h>
23
#include <linux/clk.h>
24
#include <linux/err.h>
25
#include <linux/of.h>
26
#include <mach/bridge-regs.h>
27 28 29 30

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

35
#define WDT_MAX_CYCLE_COUNT	0xffffffff
36

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

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

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

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

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

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

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

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

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

80 81 82 83 84 85 86 87 88 89
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;
}

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

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

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

107 108 109 110 111 112 113
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,
114 115
};

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

B
Bill Pemberton 已提交
122
static int orion_wdt_probe(struct platform_device *pdev)
123
{
124
	struct resource *res;
125 126
	int ret;

127
	clk = devm_clk_get(&pdev->dev, NULL);
128
	if (IS_ERR(clk)) {
129
		dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
130
		return PTR_ERR(clk);
131
	}
132 133 134
	ret = clk_prepare_enable(clk);
	if (ret)
		return ret;
135
	wdt_tclk = clk_get_rate(clk);
136

137
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 139 140 141 142
	if (!res) {
		ret = -ENODEV;
		goto disable_clk;
	}

143
	wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
144 145 146 147
	if (!wdt_reg) {
		ret = -ENOMEM;
		goto disable_clk;
	}
148 149

	wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
150

151
	orion_wdt.timeout = wdt_max_duration;
152
	orion_wdt.max_timeout = wdt_max_duration;
153
	watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
154

155 156 157 158 159 160 161 162 163
	/*
	 * 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);

164 165
	watchdog_set_nowayout(&orion_wdt, nowayout);
	ret = watchdog_register_device(&orion_wdt);
166 167
	if (ret)
		goto disable_clk;
168

169
	pr_info("Initial timeout %d sec%s\n",
170
		orion_wdt.timeout, nowayout ? ", nowayout" : "");
171
	return 0;
172 173 174 175

disable_clk:
	clk_disable_unprepare(clk);
	return ret;
176 177
}

B
Bill Pemberton 已提交
178
static int orion_wdt_remove(struct platform_device *pdev)
179
{
180
	watchdog_unregister_device(&orion_wdt);
181
	clk_disable_unprepare(clk);
182
	return 0;
183 184
}

185
static void orion_wdt_shutdown(struct platform_device *pdev)
186
{
187
	orion_wdt_stop(&orion_wdt);
188 189
}

190
static const struct of_device_id orion_wdt_of_match_table[] = {
191 192 193 194 195
	{ .compatible = "marvell,orion-wdt", },
	{},
};
MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);

196 197
static struct platform_driver orion_wdt_driver = {
	.probe		= orion_wdt_probe,
198
	.remove		= orion_wdt_remove,
199
	.shutdown	= orion_wdt_shutdown,
200 201
	.driver		= {
		.owner	= THIS_MODULE,
202
		.name	= "orion_wdt",
203
		.of_match_table = orion_wdt_of_match_table,
204 205 206
	},
};

207
module_platform_driver(orion_wdt_driver);
208 209

MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
210
MODULE_DESCRIPTION("Orion Processor Watchdog");
211 212

module_param(heartbeat, int, 0);
213
MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
214

W
Wim Van Sebroeck 已提交
215
module_param(nowayout, bool, 0);
216 217
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
218 219

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