1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/clk.h>
16#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/platform_device.h>
21#include <linux/pm.h>
22#include <linux/watchdog.h>
23
24#define WDT_START_1 0xff00
25#define WDT_START_2 0x00ff
26#define WDT_STOP_1 0xee00
27#define WDT_STOP_2 0x00ee
28
29#define WDT_TIMEOUT_REG 0x0
30#define WDT_CMD_REG 0x4
31
32#define WDT_MIN_TIMEOUT 1
33#define WDT_DEFAULT_TIMEOUT 30
34#define WDT_DEFAULT_RATE 27000000
35
36struct bcm7038_watchdog {
37 void __iomem *base;
38 struct watchdog_device wdd;
39 u32 rate;
40 struct clk *clk;
41};
42
43static bool nowayout = WATCHDOG_NOWAYOUT;
44
45static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
46{
47 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
48 u32 timeout;
49
50 timeout = wdt->rate * wdog->timeout;
51
52 writel(timeout, wdt->base + WDT_TIMEOUT_REG);
53}
54
55static int bcm7038_wdt_ping(struct watchdog_device *wdog)
56{
57 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
58
59 writel(WDT_START_1, wdt->base + WDT_CMD_REG);
60 writel(WDT_START_2, wdt->base + WDT_CMD_REG);
61
62 return 0;
63}
64
65static int bcm7038_wdt_start(struct watchdog_device *wdog)
66{
67 bcm7038_wdt_set_timeout_reg(wdog);
68 bcm7038_wdt_ping(wdog);
69
70 return 0;
71}
72
73static int bcm7038_wdt_stop(struct watchdog_device *wdog)
74{
75 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
76
77 writel(WDT_STOP_1, wdt->base + WDT_CMD_REG);
78 writel(WDT_STOP_2, wdt->base + WDT_CMD_REG);
79
80 return 0;
81}
82
83static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
84 unsigned int t)
85{
86
87 bcm7038_wdt_stop(wdog);
88 wdog->timeout = t;
89 bcm7038_wdt_start(wdog);
90
91 return 0;
92}
93
94static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
95{
96 struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
97 u32 time_left;
98
99 time_left = readl(wdt->base + WDT_CMD_REG);
100
101 return time_left / wdt->rate;
102}
103
104static const struct watchdog_info bcm7038_wdt_info = {
105 .identity = "Broadcom BCM7038 Watchdog Timer",
106 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
107 WDIOF_MAGICCLOSE
108};
109
110static const struct watchdog_ops bcm7038_wdt_ops = {
111 .owner = THIS_MODULE,
112 .start = bcm7038_wdt_start,
113 .stop = bcm7038_wdt_stop,
114 .set_timeout = bcm7038_wdt_set_timeout,
115 .get_timeleft = bcm7038_wdt_get_timeleft,
116};
117
118static int bcm7038_wdt_probe(struct platform_device *pdev)
119{
120 struct device *dev = &pdev->dev;
121 struct bcm7038_watchdog *wdt;
122 struct resource *res;
123 int err;
124
125 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
126 if (!wdt)
127 return -ENOMEM;
128
129 platform_set_drvdata(pdev, wdt);
130
131 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 wdt->base = devm_ioremap_resource(dev, res);
133 if (IS_ERR(wdt->base))
134 return PTR_ERR(wdt->base);
135
136 wdt->clk = devm_clk_get(dev, NULL);
137
138 if (!IS_ERR(wdt->clk)) {
139 err = clk_prepare_enable(wdt->clk);
140 if (err)
141 return err;
142 wdt->rate = clk_get_rate(wdt->clk);
143
144 if (!wdt->rate)
145 wdt->rate = WDT_DEFAULT_RATE;
146 } else {
147 wdt->rate = WDT_DEFAULT_RATE;
148 wdt->clk = NULL;
149 }
150
151 wdt->wdd.info = &bcm7038_wdt_info;
152 wdt->wdd.ops = &bcm7038_wdt_ops;
153 wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
154 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
155 wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
156 wdt->wdd.parent = dev;
157 watchdog_set_drvdata(&wdt->wdd, wdt);
158
159 err = watchdog_register_device(&wdt->wdd);
160 if (err) {
161 dev_err(dev, "Failed to register watchdog device\n");
162 clk_disable_unprepare(wdt->clk);
163 return err;
164 }
165
166 dev_info(dev, "Registered BCM7038 Watchdog\n");
167
168 return 0;
169}
170
171static int bcm7038_wdt_remove(struct platform_device *pdev)
172{
173 struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
174
175 if (!nowayout)
176 bcm7038_wdt_stop(&wdt->wdd);
177
178 watchdog_unregister_device(&wdt->wdd);
179 clk_disable_unprepare(wdt->clk);
180
181 return 0;
182}
183
184#ifdef CONFIG_PM_SLEEP
185static int bcm7038_wdt_suspend(struct device *dev)
186{
187 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
188
189 if (watchdog_active(&wdt->wdd))
190 return bcm7038_wdt_stop(&wdt->wdd);
191
192 return 0;
193}
194
195static int bcm7038_wdt_resume(struct device *dev)
196{
197 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
198
199 if (watchdog_active(&wdt->wdd))
200 return bcm7038_wdt_start(&wdt->wdd);
201
202 return 0;
203}
204#endif
205
206static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
207 bcm7038_wdt_resume);
208
209static void bcm7038_wdt_shutdown(struct platform_device *pdev)
210{
211 struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
212
213 if (watchdog_active(&wdt->wdd))
214 bcm7038_wdt_stop(&wdt->wdd);
215}
216
217static const struct of_device_id bcm7038_wdt_match[] = {
218 { .compatible = "brcm,bcm7038-wdt" },
219 {},
220};
221MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
222
223static struct platform_driver bcm7038_wdt_driver = {
224 .probe = bcm7038_wdt_probe,
225 .remove = bcm7038_wdt_remove,
226 .shutdown = bcm7038_wdt_shutdown,
227 .driver = {
228 .name = "bcm7038-wdt",
229 .of_match_table = bcm7038_wdt_match,
230 .pm = &bcm7038_wdt_pm_ops,
231 }
232};
233module_platform_driver(bcm7038_wdt_driver);
234
235module_param(nowayout, bool, 0);
236MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
237 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
238MODULE_LICENSE("GPL v2");
239MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
240MODULE_AUTHOR("Justin Chen");
241