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