blob: e238df4d75a23fdbd54d7e242452d8a6f5c8d1fd [file] [log] [blame]
Justin Chen7a3629f2015-08-31 11:02:43 -07001/*
2 * Copyright (C) 2015 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
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 /* seconds */
33#define WDT_DEFAULT_TIMEOUT 30 /* seconds */
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 /* Can't modify timeout value if watchdog timer is running */
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 struct watchdog_info bcm7038_wdt_info = {
105 .identity = "Broadcom BCM7038 Watchdog Timer",
106 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
107 WDIOF_MAGICCLOSE
108};
109
Julia Lawall85f15cf2016-09-01 19:35:26 +0200110static const struct watchdog_ops bcm7038_wdt_ops = {
Justin Chen7a3629f2015-08-31 11:02:43 -0700111 .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 /* If unable to get clock, use default frequency */
138 if (!IS_ERR(wdt->clk)) {
139 clk_prepare_enable(wdt->clk);
140 wdt->rate = clk_get_rate(wdt->clk);
141 /* Prevent divide-by-zero exception */
142 if (!wdt->rate)
143 wdt->rate = WDT_DEFAULT_RATE;
144 } else {
145 wdt->rate = WDT_DEFAULT_RATE;
146 wdt->clk = NULL;
147 }
148
149 wdt->wdd.info = &bcm7038_wdt_info;
150 wdt->wdd.ops = &bcm7038_wdt_ops;
151 wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
152 wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
153 wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
154 wdt->wdd.parent = dev;
155 watchdog_set_drvdata(&wdt->wdd, wdt);
156
157 err = watchdog_register_device(&wdt->wdd);
158 if (err) {
159 dev_err(dev, "Failed to register watchdog device\n");
160 clk_disable_unprepare(wdt->clk);
161 return err;
162 }
163
164 dev_info(dev, "Registered BCM7038 Watchdog\n");
165
166 return 0;
167}
168
169static int bcm7038_wdt_remove(struct platform_device *pdev)
170{
171 struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
172
173 if (!nowayout)
174 bcm7038_wdt_stop(&wdt->wdd);
175
176 watchdog_unregister_device(&wdt->wdd);
177 clk_disable_unprepare(wdt->clk);
178
179 return 0;
180}
181
182#ifdef CONFIG_PM_SLEEP
183static int bcm7038_wdt_suspend(struct device *dev)
184{
185 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
186
187 if (watchdog_active(&wdt->wdd))
188 return bcm7038_wdt_stop(&wdt->wdd);
189
190 return 0;
191}
192
193static int bcm7038_wdt_resume(struct device *dev)
194{
195 struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
196
197 if (watchdog_active(&wdt->wdd))
198 return bcm7038_wdt_start(&wdt->wdd);
199
200 return 0;
201}
202#endif
203
204static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
205 bcm7038_wdt_resume);
206
207static void bcm7038_wdt_shutdown(struct platform_device *pdev)
208{
209 struct bcm7038_watchdog *wdt = platform_get_drvdata(pdev);
210
211 if (watchdog_active(&wdt->wdd))
212 bcm7038_wdt_stop(&wdt->wdd);
213}
214
215static const struct of_device_id bcm7038_wdt_match[] = {
216 { .compatible = "brcm,bcm7038-wdt" },
217 {},
218};
219
220static struct platform_driver bcm7038_wdt_driver = {
221 .probe = bcm7038_wdt_probe,
222 .remove = bcm7038_wdt_remove,
223 .shutdown = bcm7038_wdt_shutdown,
224 .driver = {
225 .name = "bcm7038-wdt",
226 .of_match_table = bcm7038_wdt_match,
227 .pm = &bcm7038_wdt_pm_ops,
228 }
229};
230module_platform_driver(bcm7038_wdt_driver);
231
232module_param(nowayout, bool, 0);
233MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
234 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
235MODULE_LICENSE("GPL v2");
236MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
237MODULE_AUTHOR("Justin Chen");