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