summaryrefslogtreecommitdiff
path: root/target/linux/mcs814x/files-3.3/drivers/watchdog/mcs814x_wdt.c
blob: ca69e8f955187d2e115fae523c94bd9cf98f2e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Moschip MCS814x Watchdog driver
 *
 * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/of.h>

#define WDT_COUNT	0x00
#define WDT_CTRL	0x04
#define  WDT_CTRL_EN	0x1

/* watchdog frequency */
#define WDT_MAX_VALUE	(0xffffffff)

struct mcs814x_wdt {
	void __iomem		*regs;
	spinlock_t		lock;
	struct watchdog_device	wdt_dev;
	struct clk		*clk;
};

static int mcs814x_wdt_start(struct watchdog_device *dev)
{
	struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
	u32 reg;

	spin_lock(&wdt->lock);
	reg = readl_relaxed(wdt->regs + WDT_CTRL);
	reg |= WDT_CTRL_EN;
	writel_relaxed(reg, wdt->regs + WDT_CTRL);
	spin_unlock(&wdt->lock);

	return 0;
}

static int mcs814x_wdt_stop(struct watchdog_device *dev)
{
	struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
	u32 reg;

	spin_lock(&wdt->lock);
	reg = readl_relaxed(wdt->regs + WDT_CTRL);
	reg &= ~WDT_CTRL_EN;
	writel_relaxed(reg, wdt->regs + WDT_CTRL);
	spin_unlock(&wdt->lock);

	return 0;
}

static int mcs814x_wdt_set_timeout(struct watchdog_device *dev,
				unsigned int new_timeout)
{
	struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);

	spin_lock(&wdt->lock);
	/* watchdog counts upward and rollover (0xfffffff -> 0)
	 * triggers the reboot
	 */
	writel_relaxed(WDT_MAX_VALUE - (new_timeout * clk_get_rate(wdt->clk)),
		wdt->regs + WDT_COUNT);
	spin_unlock(&wdt->lock);

	return 0;
}

static int mcs814x_wdt_ping(struct watchdog_device *dev)
{
	/* restart the watchdog */
	mcs814x_wdt_stop(dev);
	mcs814x_wdt_set_timeout(dev, dev->timeout);
	mcs814x_wdt_start(dev);

	return 0;
}

static const struct watchdog_info mcs814x_wdt_ident = {
	.options	= WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
			  WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
	.identity	= "MCS814x Watchdog",
};

static struct watchdog_ops mcs814x_wdt_ops = {
	.owner		= THIS_MODULE,
	.start		= mcs814x_wdt_start,
	.stop		= mcs814x_wdt_stop,
	.set_timeout	= mcs814x_wdt_set_timeout,
	.ping		= mcs814x_wdt_ping,
};

static int __devinit mcs814x_wdt_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct mcs814x_wdt *wdt;
	int ret;
	struct clk *clk;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;

	clk = clk_get(NULL, "wdt");
	if (IS_ERR_OR_NULL(clk)) {
		dev_err(&pdev->dev, "failed to get watchdog clock\n");
		return PTR_ERR(clk);
	}

	wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
	if (!wdt) {
		ret = -ENOMEM;
		goto out_clk;
	}

	spin_lock_init(&wdt->lock);
	wdt->clk = clk;
	wdt->wdt_dev.info = &mcs814x_wdt_ident;
	wdt->wdt_dev.ops = &mcs814x_wdt_ops;
	wdt->wdt_dev.min_timeout = 1;
	/* approximately 10995 secs */
	wdt->wdt_dev.max_timeout = (WDT_MAX_VALUE / clk_get_rate(clk));

	platform_set_drvdata(pdev, wdt);

	/* only ioremap registers, because the register is shared */
	wdt->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (!wdt->regs) {
		ret = -ENOMEM;
		goto out;
	}

	watchdog_set_drvdata(&wdt->wdt_dev, wdt);

	ret = watchdog_register_device(&wdt->wdt_dev);
	if (ret) {
		dev_err(&pdev->dev, "cannot register watchdog: %d\n", ret);
		goto out;
	}

	dev_info(&pdev->dev, "registered\n");
	return 0;

out:
	platform_set_drvdata(pdev, NULL);
	kfree(wdt);
out_clk:
	clk_put(clk);
	return ret;
}

static int __devexit mcs814x_wdt_remove(struct platform_device *pdev)
{
	struct mcs814x_wdt *wdt = platform_get_drvdata(pdev);

	clk_put(wdt->clk);
	watchdog_unregister_device(&wdt->wdt_dev);
	watchdog_set_drvdata(&wdt->wdt_dev, NULL);
	kfree(wdt);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

static const struct of_device_id mcs814x_wdt_ids[] = {
	{ .compatible = "moschip,mcs814x-wdt", },
	{ /* sentinel */ },
};

static struct platform_driver mcs814x_wdt_driver = {
	.driver	= {
		.name	= "mcs814x-wdt",
		.owner	= THIS_MODULE,
		.of_match_table = mcs814x_wdt_ids,
	},
	.probe		= mcs814x_wdt_probe,
	.remove		= __devexit_p(mcs814x_wdt_remove),
};

module_platform_driver(mcs814x_wdt_driver);

MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
MODULE_DESCRIPTION("Moschip MCS814x Watchdog driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
MODULE_ALIAS("platform:mcs814x-wdt");