2 * Copyright (C) 2015 Broadcom Corporation
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.
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.
15 #include <linux/clk.h>
16 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
22 #include <linux/watchdog.h>
24 #define WDT_START_1 0xff00
25 #define WDT_START_2 0x00ff
26 #define WDT_STOP_1 0xee00
27 #define WDT_STOP_2 0x00ee
29 #define WDT_TIMEOUT_REG 0x0
30 #define WDT_CMD_REG 0x4
32 #define WDT_MIN_TIMEOUT 1 /* seconds */
33 #define WDT_DEFAULT_TIMEOUT 30 /* seconds */
34 #define WDT_DEFAULT_RATE 27000000
36 struct bcm7038_watchdog
{
38 struct watchdog_device wdd
;
43 static bool nowayout
= WATCHDOG_NOWAYOUT
;
45 static void bcm7038_wdt_set_timeout_reg(struct watchdog_device
*wdog
)
47 struct bcm7038_watchdog
*wdt
= watchdog_get_drvdata(wdog
);
50 timeout
= wdt
->rate
* wdog
->timeout
;
52 writel(timeout
, wdt
->base
+ WDT_TIMEOUT_REG
);
55 static int bcm7038_wdt_ping(struct watchdog_device
*wdog
)
57 struct bcm7038_watchdog
*wdt
= watchdog_get_drvdata(wdog
);
59 writel(WDT_START_1
, wdt
->base
+ WDT_CMD_REG
);
60 writel(WDT_START_2
, wdt
->base
+ WDT_CMD_REG
);
65 static int bcm7038_wdt_start(struct watchdog_device
*wdog
)
67 bcm7038_wdt_set_timeout_reg(wdog
);
68 bcm7038_wdt_ping(wdog
);
73 static int bcm7038_wdt_stop(struct watchdog_device
*wdog
)
75 struct bcm7038_watchdog
*wdt
= watchdog_get_drvdata(wdog
);
77 writel(WDT_STOP_1
, wdt
->base
+ WDT_CMD_REG
);
78 writel(WDT_STOP_2
, wdt
->base
+ WDT_CMD_REG
);
83 static int bcm7038_wdt_set_timeout(struct watchdog_device
*wdog
,
86 /* Can't modify timeout value if watchdog timer is running */
87 bcm7038_wdt_stop(wdog
);
89 bcm7038_wdt_start(wdog
);
94 static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device
*wdog
)
96 struct bcm7038_watchdog
*wdt
= watchdog_get_drvdata(wdog
);
99 time_left
= readl(wdt
->base
+ WDT_CMD_REG
);
101 return time_left
/ wdt
->rate
;
104 static const struct watchdog_info bcm7038_wdt_info
= {
105 .identity
= "Broadcom BCM7038 Watchdog Timer",
106 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
|
110 static 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
,
118 static int bcm7038_wdt_probe(struct platform_device
*pdev
)
120 struct device
*dev
= &pdev
->dev
;
121 struct bcm7038_watchdog
*wdt
;
122 struct resource
*res
;
125 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
129 platform_set_drvdata(pdev
, wdt
);
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
);
136 wdt
->clk
= devm_clk_get(dev
, NULL
);
137 /* If unable to get clock, use default frequency */
138 if (!IS_ERR(wdt
->clk
)) {
139 err
= clk_prepare_enable(wdt
->clk
);
142 wdt
->rate
= clk_get_rate(wdt
->clk
);
143 /* Prevent divide-by-zero exception */
145 wdt
->rate
= WDT_DEFAULT_RATE
;
147 wdt
->rate
= WDT_DEFAULT_RATE
;
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
);
159 err
= watchdog_register_device(&wdt
->wdd
);
161 dev_err(dev
, "Failed to register watchdog device\n");
162 clk_disable_unprepare(wdt
->clk
);
166 dev_info(dev
, "Registered BCM7038 Watchdog\n");
171 static int bcm7038_wdt_remove(struct platform_device
*pdev
)
173 struct bcm7038_watchdog
*wdt
= platform_get_drvdata(pdev
);
176 bcm7038_wdt_stop(&wdt
->wdd
);
178 watchdog_unregister_device(&wdt
->wdd
);
179 clk_disable_unprepare(wdt
->clk
);
184 #ifdef CONFIG_PM_SLEEP
185 static int bcm7038_wdt_suspend(struct device
*dev
)
187 struct bcm7038_watchdog
*wdt
= dev_get_drvdata(dev
);
189 if (watchdog_active(&wdt
->wdd
))
190 return bcm7038_wdt_stop(&wdt
->wdd
);
195 static int bcm7038_wdt_resume(struct device
*dev
)
197 struct bcm7038_watchdog
*wdt
= dev_get_drvdata(dev
);
199 if (watchdog_active(&wdt
->wdd
))
200 return bcm7038_wdt_start(&wdt
->wdd
);
206 static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops
, bcm7038_wdt_suspend
,
209 static void bcm7038_wdt_shutdown(struct platform_device
*pdev
)
211 struct bcm7038_watchdog
*wdt
= platform_get_drvdata(pdev
);
213 if (watchdog_active(&wdt
->wdd
))
214 bcm7038_wdt_stop(&wdt
->wdd
);
217 static const struct of_device_id bcm7038_wdt_match
[] = {
218 { .compatible
= "brcm,bcm7038-wdt" },
221 MODULE_DEVICE_TABLE(of
, bcm7038_wdt_match
);
223 static struct platform_driver bcm7038_wdt_driver
= {
224 .probe
= bcm7038_wdt_probe
,
225 .remove
= bcm7038_wdt_remove
,
226 .shutdown
= bcm7038_wdt_shutdown
,
228 .name
= "bcm7038-wdt",
229 .of_match_table
= bcm7038_wdt_match
,
230 .pm
= &bcm7038_wdt_pm_ops
,
233 module_platform_driver(bcm7038_wdt_driver
);
235 module_param(nowayout
, bool, 0);
236 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
237 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
238 MODULE_LICENSE("GPL v2");
239 MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
240 MODULE_AUTHOR("Justin Chen");