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 clk_prepare_enable(wdt
->clk
);
140 wdt
->rate
= clk_get_rate(wdt
->clk
);
141 /* Prevent divide-by-zero exception */
143 wdt
->rate
= WDT_DEFAULT_RATE
;
145 wdt
->rate
= WDT_DEFAULT_RATE
;
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
);
157 err
= watchdog_register_device(&wdt
->wdd
);
159 dev_err(dev
, "Failed to register watchdog device\n");
160 clk_disable_unprepare(wdt
->clk
);
164 dev_info(dev
, "Registered BCM7038 Watchdog\n");
169 static int bcm7038_wdt_remove(struct platform_device
*pdev
)
171 struct bcm7038_watchdog
*wdt
= platform_get_drvdata(pdev
);
174 bcm7038_wdt_stop(&wdt
->wdd
);
176 watchdog_unregister_device(&wdt
->wdd
);
177 clk_disable_unprepare(wdt
->clk
);
182 #ifdef CONFIG_PM_SLEEP
183 static int bcm7038_wdt_suspend(struct device
*dev
)
185 struct bcm7038_watchdog
*wdt
= dev_get_drvdata(dev
);
187 if (watchdog_active(&wdt
->wdd
))
188 return bcm7038_wdt_stop(&wdt
->wdd
);
193 static int bcm7038_wdt_resume(struct device
*dev
)
195 struct bcm7038_watchdog
*wdt
= dev_get_drvdata(dev
);
197 if (watchdog_active(&wdt
->wdd
))
198 return bcm7038_wdt_start(&wdt
->wdd
);
204 static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops
, bcm7038_wdt_suspend
,
207 static void bcm7038_wdt_shutdown(struct platform_device
*pdev
)
209 struct bcm7038_watchdog
*wdt
= platform_get_drvdata(pdev
);
211 if (watchdog_active(&wdt
->wdd
))
212 bcm7038_wdt_stop(&wdt
->wdd
);
215 static const struct of_device_id bcm7038_wdt_match
[] = {
216 { .compatible
= "brcm,bcm7038-wdt" },
219 MODULE_DEVICE_TABLE(of
, bcm7038_wdt_match
);
221 static struct platform_driver bcm7038_wdt_driver
= {
222 .probe
= bcm7038_wdt_probe
,
223 .remove
= bcm7038_wdt_remove
,
224 .shutdown
= bcm7038_wdt_shutdown
,
226 .name
= "bcm7038-wdt",
227 .of_match_table
= bcm7038_wdt_match
,
228 .pm
= &bcm7038_wdt_pm_ops
,
231 module_platform_driver(bcm7038_wdt_driver
);
233 module_param(nowayout
, bool, 0);
234 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
235 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
236 MODULE_LICENSE("GPL v2");
237 MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
238 MODULE_AUTHOR("Justin Chen");