Merge branch 'for-linville' of git://github.com/kvalo/ath
[linux-2.6-block.git] / drivers / watchdog / moxart_wdt.c
CommitLineData
e14538e0
JJ
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/platform_device.h>
19#include <linux/watchdog.h>
20#include <linux/moduleparam.h>
21
8773926d
JJ
22#include <asm/system_misc.h>
23
e14538e0
JJ
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};
33
8773926d
JJ
34static struct moxart_wdt_dev *moxart_restart_ctx;
35
e14538e0
JJ
36static int heartbeat;
37
8773926d
JJ
38static void moxart_wdt_restart(enum reboot_mode reboot_mode, const char *cmd)
39{
40 writel(1, moxart_restart_ctx->base + REG_COUNT);
41 writel(0x5ab9, moxart_restart_ctx->base + REG_MODE);
42 writel(0x03, moxart_restart_ctx->base + REG_ENABLE);
43}
44
e14538e0
JJ
45static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
46{
47 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
48
49 writel(0, moxart_wdt->base + REG_ENABLE);
50
51 return 0;
52}
53
54static int moxart_wdt_start(struct watchdog_device *wdt_dev)
55{
56 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
57
58 writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
59 moxart_wdt->base + REG_COUNT);
60 writel(0x5ab9, moxart_wdt->base + REG_MODE);
61 writel(0x03, moxart_wdt->base + REG_ENABLE);
62
63 return 0;
64}
65
66static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
67 unsigned int timeout)
68{
69 wdt_dev->timeout = timeout;
70
71 return 0;
72}
73
74static const struct watchdog_info moxart_wdt_info = {
75 .identity = "moxart-wdt",
76 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
77 WDIOF_MAGICCLOSE,
78};
79
80static const struct watchdog_ops moxart_wdt_ops = {
81 .owner = THIS_MODULE,
82 .start = moxart_wdt_start,
83 .stop = moxart_wdt_stop,
84 .set_timeout = moxart_wdt_set_timeout,
85};
86
87static int moxart_wdt_probe(struct platform_device *pdev)
88{
89 struct moxart_wdt_dev *moxart_wdt;
90 struct device *dev = &pdev->dev;
91 struct device_node *node = dev->of_node;
92 struct resource *res;
93 struct clk *clk;
94 int err;
95 unsigned int max_timeout;
96 bool nowayout = WATCHDOG_NOWAYOUT;
97
98 moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
99 if (!moxart_wdt)
100 return -ENOMEM;
101
102 platform_set_drvdata(pdev, moxart_wdt);
103
104 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 moxart_wdt->base = devm_ioremap_resource(dev, res);
106 if (IS_ERR(moxart_wdt->base))
107 return PTR_ERR(moxart_wdt->base);
108
109 clk = of_clk_get(node, 0);
110 if (IS_ERR(clk)) {
111 pr_err("%s: of_clk_get failed\n", __func__);
112 return PTR_ERR(clk);
113 }
114
115 moxart_wdt->clock_frequency = clk_get_rate(clk);
116 if (moxart_wdt->clock_frequency == 0) {
117 pr_err("%s: incorrect clock frequency\n", __func__);
118 return -EINVAL;
119 }
120
121 max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
122
123 moxart_wdt->dev.info = &moxart_wdt_info;
124 moxart_wdt->dev.ops = &moxart_wdt_ops;
125 moxart_wdt->dev.timeout = max_timeout;
126 moxart_wdt->dev.min_timeout = 1;
127 moxart_wdt->dev.max_timeout = max_timeout;
128 moxart_wdt->dev.parent = dev;
129
130 watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
131 watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
132
133 watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
134
135 err = watchdog_register_device(&moxart_wdt->dev);
136 if (err)
137 return err;
138
8773926d
JJ
139 moxart_restart_ctx = moxart_wdt;
140 arm_pm_restart = moxart_wdt_restart;
141
e14538e0
JJ
142 dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
143 moxart_wdt->dev.timeout, nowayout);
144
145 return 0;
146}
147
148static int moxart_wdt_remove(struct platform_device *pdev)
149{
150 struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
151
8773926d 152 arm_pm_restart = NULL;
e14538e0
JJ
153 moxart_wdt_stop(&moxart_wdt->dev);
154 watchdog_unregister_device(&moxart_wdt->dev);
155
156 return 0;
157}
158
159static const struct of_device_id moxart_watchdog_match[] = {
160 { .compatible = "moxa,moxart-watchdog" },
161 { },
162};
163
164static struct platform_driver moxart_wdt_driver = {
165 .probe = moxart_wdt_probe,
166 .remove = moxart_wdt_remove,
167 .driver = {
168 .name = "moxart-watchdog",
169 .owner = THIS_MODULE,
170 .of_match_table = moxart_watchdog_match,
171 },
172};
173module_platform_driver(moxart_wdt_driver);
174
175module_param(heartbeat, int, 0);
176MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
177
178MODULE_DESCRIPTION("MOXART watchdog driver");
179MODULE_LICENSE("GPL");
180MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");