orion_wdt.c 4.8 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
static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
81
{
82
	return readl(wdt_reg + WDT_VAL) / wdt_tclk;
83 84
}

85 86
static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
				 unsigned int timeout)
87
{
88
	wdt_dev->timeout = timeout;
89 90 91
	return 0;
}

92 93 94
static const struct watchdog_info orion_wdt_info = {
	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
	.identity = "Orion Watchdog",
95 96
};

97 98 99 100 101 102 103
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,
104 105
};

106 107 108
static struct watchdog_device orion_wdt = {
	.info = &orion_wdt_info,
	.ops = &orion_wdt_ops,
109
	.min_timeout = 1,
110 111
};

B
Bill Pemberton 已提交
112
static int orion_wdt_probe(struct platform_device *pdev)
113
{
114
	struct resource *res;
115 116
	int ret;

117
	clk = devm_clk_get(&pdev->dev, NULL);
118
	if (IS_ERR(clk)) {
119
		dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
120
		return PTR_ERR(clk);
121
	}
122 123 124
	ret = clk_prepare_enable(clk);
	if (ret)
		return ret;
125
	wdt_tclk = clk_get_rate(clk);
126

127
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 129 130 131 132
	if (!res) {
		ret = -ENODEV;
		goto disable_clk;
	}

133
	wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
134 135 136 137
	if (!wdt_reg) {
		ret = -ENOMEM;
		goto disable_clk;
	}
138 139

	wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
140

141
	orion_wdt.timeout = wdt_max_duration;
142
	orion_wdt.max_timeout = wdt_max_duration;
143
	watchdog_init_timeout(&orion_wdt, heartbeat, &pdev->dev);
144 145 146

	watchdog_set_nowayout(&orion_wdt, nowayout);
	ret = watchdog_register_device(&orion_wdt);
147 148
	if (ret)
		goto disable_clk;
149

150
	pr_info("Initial timeout %d sec%s\n",
151
		orion_wdt.timeout, nowayout ? ", nowayout" : "");
152
	return 0;
153 154 155 156

disable_clk:
	clk_disable_unprepare(clk);
	return ret;
157 158
}

B
Bill Pemberton 已提交
159
static int orion_wdt_remove(struct platform_device *pdev)
160
{
161
	watchdog_unregister_device(&orion_wdt);
162
	clk_disable_unprepare(clk);
163
	return 0;
164 165
}

166
static void orion_wdt_shutdown(struct platform_device *pdev)
167
{
168
	orion_wdt_stop(&orion_wdt);
169 170
}

171
static const struct of_device_id orion_wdt_of_match_table[] = {
172 173 174 175 176
	{ .compatible = "marvell,orion-wdt", },
	{},
};
MODULE_DEVICE_TABLE(of, orion_wdt_of_match_table);

177 178
static struct platform_driver orion_wdt_driver = {
	.probe		= orion_wdt_probe,
179
	.remove		= orion_wdt_remove,
180
	.shutdown	= orion_wdt_shutdown,
181 182
	.driver		= {
		.owner	= THIS_MODULE,
183
		.name	= "orion_wdt",
184
		.of_match_table = orion_wdt_of_match_table,
185 186 187
	},
};

188
module_platform_driver(orion_wdt_driver);
189 190

MODULE_AUTHOR("Sylver Bruneau <sylver.bruneau@googlemail.com>");
191
MODULE_DESCRIPTION("Orion Processor Watchdog");
192 193

module_param(heartbeat, int, 0);
194
MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
195

W
Wim Van Sebroeck 已提交
196
module_param(nowayout, bool, 0);
197 198
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
199 200

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