blob: 2c4a73d1e2144393c52c7e259c61265b27de0a15 [file] [log] [blame]
Jonas Jensene14538e2013-08-02 16:40:45 +02001/*
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
22#define REG_COUNT 0x4
23#define REG_MODE 0x8
24#define REG_ENABLE 0xC
25
26struct moxart_wdt_dev {
27 struct watchdog_device dev;
28 void __iomem *base;
29 unsigned int clock_frequency;
30};
31
32static int heartbeat;
33
Guenter Roeck4d8b2292016-02-26 17:32:49 -080034static int moxart_wdt_restart(struct watchdog_device *wdt_dev,
35 unsigned long action, void *data)
Jonas Jensen87739262013-12-18 16:18:03 +010036{
Damien Riegel46c17f02015-11-16 12:28:07 -050037 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
38
Guenter Roeckad0e0e62014-09-26 00:03:17 +000039 writel(1, moxart_wdt->base + REG_COUNT);
40 writel(0x5ab9, moxart_wdt->base + REG_MODE);
41 writel(0x03, moxart_wdt->base + REG_ENABLE);
42
Damien Riegel46c17f02015-11-16 12:28:07 -050043 return 0;
Jonas Jensen87739262013-12-18 16:18:03 +010044}
45
Jonas Jensene14538e2013-08-02 16:40:45 +020046static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
47{
48 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
49
50 writel(0, moxart_wdt->base + REG_ENABLE);
51
52 return 0;
53}
54
55static int moxart_wdt_start(struct watchdog_device *wdt_dev)
56{
57 struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
58
59 writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
60 moxart_wdt->base + REG_COUNT);
61 writel(0x5ab9, moxart_wdt->base + REG_MODE);
62 writel(0x03, moxart_wdt->base + REG_ENABLE);
63
64 return 0;
65}
66
67static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
68 unsigned int timeout)
69{
70 wdt_dev->timeout = timeout;
71
72 return 0;
73}
74
75static const struct watchdog_info moxart_wdt_info = {
76 .identity = "moxart-wdt",
77 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
78 WDIOF_MAGICCLOSE,
79};
80
81static const struct watchdog_ops moxart_wdt_ops = {
82 .owner = THIS_MODULE,
83 .start = moxart_wdt_start,
84 .stop = moxart_wdt_stop,
85 .set_timeout = moxart_wdt_set_timeout,
Damien Riegel46c17f02015-11-16 12:28:07 -050086 .restart = moxart_wdt_restart,
Jonas Jensene14538e2013-08-02 16:40:45 +020087};
88
89static int moxart_wdt_probe(struct platform_device *pdev)
90{
91 struct moxart_wdt_dev *moxart_wdt;
92 struct device *dev = &pdev->dev;
93 struct device_node *node = dev->of_node;
94 struct resource *res;
95 struct clk *clk;
96 int err;
97 unsigned int max_timeout;
98 bool nowayout = WATCHDOG_NOWAYOUT;
99
100 moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
101 if (!moxart_wdt)
102 return -ENOMEM;
103
104 platform_set_drvdata(pdev, moxart_wdt);
105
106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
107 moxart_wdt->base = devm_ioremap_resource(dev, res);
108 if (IS_ERR(moxart_wdt->base))
109 return PTR_ERR(moxart_wdt->base);
110
111 clk = of_clk_get(node, 0);
112 if (IS_ERR(clk)) {
113 pr_err("%s: of_clk_get failed\n", __func__);
114 return PTR_ERR(clk);
115 }
116
117 moxart_wdt->clock_frequency = clk_get_rate(clk);
118 if (moxart_wdt->clock_frequency == 0) {
119 pr_err("%s: incorrect clock frequency\n", __func__);
120 return -EINVAL;
121 }
122
123 max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
124
125 moxart_wdt->dev.info = &moxart_wdt_info;
126 moxart_wdt->dev.ops = &moxart_wdt_ops;
127 moxart_wdt->dev.timeout = max_timeout;
128 moxart_wdt->dev.min_timeout = 1;
129 moxart_wdt->dev.max_timeout = max_timeout;
130 moxart_wdt->dev.parent = dev;
131
132 watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
133 watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
Damien Riegel46c17f02015-11-16 12:28:07 -0500134 watchdog_set_restart_priority(&moxart_wdt->dev, 128);
Jonas Jensene14538e2013-08-02 16:40:45 +0200135
136 watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
137
138 err = watchdog_register_device(&moxart_wdt->dev);
139 if (err)
140 return err;
141
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
Jonas Jensene14538e2013-08-02 16:40:45 +0200152 moxart_wdt_stop(&moxart_wdt->dev);
Jonas Jensene14538e2013-08-02 16:40:45 +0200153
154 return 0;
155}
156
157static const struct of_device_id moxart_watchdog_match[] = {
158 { .compatible = "moxa,moxart-watchdog" },
159 { },
160};
Luis de Bethencourtc73318f2015-09-03 13:06:09 +0200161MODULE_DEVICE_TABLE(of, moxart_watchdog_match);
Jonas Jensene14538e2013-08-02 16:40:45 +0200162
163static struct platform_driver moxart_wdt_driver = {
164 .probe = moxart_wdt_probe,
165 .remove = moxart_wdt_remove,
166 .driver = {
167 .name = "moxart-watchdog",
Jonas Jensene14538e2013-08-02 16:40:45 +0200168 .of_match_table = moxart_watchdog_match,
169 },
170};
171module_platform_driver(moxart_wdt_driver);
172
173module_param(heartbeat, int, 0);
174MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
175
176MODULE_DESCRIPTION("MOXART watchdog driver");
177MODULE_LICENSE("GPL");
178MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");