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