1/*
2 * MOXA ART SoCs watchdog driver.
3 *
4 * Copyright (C) 2013 Jonas Jensen
5 *
6 * Jonas Jensen <jonas.jensen@gmail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2.  This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/clk.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/err.h>
17#include <linux/kernel.h>
18#include <linux/notifier.h>
19#include <linux/platform_device.h>
20#include <linux/reboot.h>
21#include <linux/watchdog.h>
22#include <linux/moduleparam.h>
23
24#define REG_COUNT			0x4
25#define REG_MODE			0x8
26#define REG_ENABLE			0xC
27
28struct moxart_wdt_dev {
29	struct watchdog_device dev;
30	void __iomem *base;
31	unsigned int clock_frequency;
32	struct notifier_block restart_handler;
33};
34
35static int heartbeat;
36
37static int moxart_restart_handle(struct notifier_block *this,
38				 unsigned long mode, void *cmd)
39{
40	struct moxart_wdt_dev *moxart_wdt = container_of(this,
41							 struct moxart_wdt_dev,
42							 restart_handler);
43	writel(1, moxart_wdt->base + REG_COUNT);
44	writel(0x5ab9, moxart_wdt->base + REG_MODE);
45	writel(0x03, moxart_wdt->base + REG_ENABLE);
46
47	return NOTIFY_DONE;
48}
49
50static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
51{
52	struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
53
54	writel(0, moxart_wdt->base + REG_ENABLE);
55
56	return 0;
57}
58
59static int moxart_wdt_start(struct watchdog_device *wdt_dev)
60{
61	struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
62
63	writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
64	       moxart_wdt->base + REG_COUNT);
65	writel(0x5ab9, moxart_wdt->base + REG_MODE);
66	writel(0x03, moxart_wdt->base + REG_ENABLE);
67
68	return 0;
69}
70
71static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
72				  unsigned int timeout)
73{
74	wdt_dev->timeout = timeout;
75
76	return 0;
77}
78
79static const struct watchdog_info moxart_wdt_info = {
80	.identity       = "moxart-wdt",
81	.options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
82			  WDIOF_MAGICCLOSE,
83};
84
85static const struct watchdog_ops moxart_wdt_ops = {
86	.owner          = THIS_MODULE,
87	.start          = moxart_wdt_start,
88	.stop           = moxart_wdt_stop,
89	.set_timeout    = moxart_wdt_set_timeout,
90};
91
92static int moxart_wdt_probe(struct platform_device *pdev)
93{
94	struct moxart_wdt_dev *moxart_wdt;
95	struct device *dev = &pdev->dev;
96	struct device_node *node = dev->of_node;
97	struct resource *res;
98	struct clk *clk;
99	int err;
100	unsigned int max_timeout;
101	bool nowayout = WATCHDOG_NOWAYOUT;
102
103	moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
104	if (!moxart_wdt)
105		return -ENOMEM;
106
107	platform_set_drvdata(pdev, moxart_wdt);
108
109	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
110	moxart_wdt->base = devm_ioremap_resource(dev, res);
111	if (IS_ERR(moxart_wdt->base))
112		return PTR_ERR(moxart_wdt->base);
113
114	clk = of_clk_get(node, 0);
115	if (IS_ERR(clk)) {
116		pr_err("%s: of_clk_get failed\n", __func__);
117		return PTR_ERR(clk);
118	}
119
120	moxart_wdt->clock_frequency = clk_get_rate(clk);
121	if (moxart_wdt->clock_frequency == 0) {
122		pr_err("%s: incorrect clock frequency\n", __func__);
123		return -EINVAL;
124	}
125
126	max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
127
128	moxart_wdt->dev.info = &moxart_wdt_info;
129	moxart_wdt->dev.ops = &moxart_wdt_ops;
130	moxart_wdt->dev.timeout = max_timeout;
131	moxart_wdt->dev.min_timeout = 1;
132	moxart_wdt->dev.max_timeout = max_timeout;
133	moxart_wdt->dev.parent = dev;
134
135	watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
136	watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
137
138	watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
139
140	err = watchdog_register_device(&moxart_wdt->dev);
141	if (err)
142		return err;
143
144	moxart_wdt->restart_handler.notifier_call = moxart_restart_handle;
145	moxart_wdt->restart_handler.priority = 128;
146	err = register_restart_handler(&moxart_wdt->restart_handler);
147	if (err)
148		dev_err(dev, "cannot register restart notifier (err=%d)\n",
149			err);
150
151	dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
152		moxart_wdt->dev.timeout, nowayout);
153
154	return 0;
155}
156
157static int moxart_wdt_remove(struct platform_device *pdev)
158{
159	struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
160
161	unregister_restart_handler(&moxart_wdt->restart_handler);
162	moxart_wdt_stop(&moxart_wdt->dev);
163
164	return 0;
165}
166
167static const struct of_device_id moxart_watchdog_match[] = {
168	{ .compatible = "moxa,moxart-watchdog" },
169	{ },
170};
171MODULE_DEVICE_TABLE(of, moxart_watchdog_match);
172
173static struct platform_driver moxart_wdt_driver = {
174	.probe      = moxart_wdt_probe,
175	.remove     = moxart_wdt_remove,
176	.driver     = {
177		.name		= "moxart-watchdog",
178		.of_match_table	= moxart_watchdog_match,
179	},
180};
181module_platform_driver(moxart_wdt_driver);
182
183module_param(heartbeat, int, 0);
184MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
185
186MODULE_DESCRIPTION("MOXART watchdog driver");
187MODULE_LICENSE("GPL");
188MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
189