1 /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/watchdog.h>
25 #define WDT_BITE_TIME 0x5C
28 struct watchdog_device wdd
;
31 struct notifier_block restart_nb
;
36 struct qcom_wdt
*to_qcom_wdt(struct watchdog_device
*wdd
)
38 return container_of(wdd
, struct qcom_wdt
, wdd
);
41 static int qcom_wdt_start(struct watchdog_device
*wdd
)
43 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
45 writel(0, wdt
->base
+ WDT_EN
);
46 writel(1, wdt
->base
+ WDT_RST
);
47 writel(wdd
->timeout
* wdt
->rate
, wdt
->base
+ WDT_BITE_TIME
);
48 writel(1, wdt
->base
+ WDT_EN
);
52 static int qcom_wdt_stop(struct watchdog_device
*wdd
)
54 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
56 writel(0, wdt
->base
+ WDT_EN
);
60 static int qcom_wdt_ping(struct watchdog_device
*wdd
)
62 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
64 writel(1, wdt
->base
+ WDT_RST
);
68 static int qcom_wdt_set_timeout(struct watchdog_device
*wdd
,
71 wdd
->timeout
= timeout
;
72 return qcom_wdt_start(wdd
);
75 static const struct watchdog_ops qcom_wdt_ops
= {
76 .start
= qcom_wdt_start
,
77 .stop
= qcom_wdt_stop
,
78 .ping
= qcom_wdt_ping
,
79 .set_timeout
= qcom_wdt_set_timeout
,
83 static const struct watchdog_info qcom_wdt_info
= {
84 .options
= WDIOF_KEEPALIVEPING
87 .identity
= KBUILD_MODNAME
,
90 static int qcom_wdt_restart(struct notifier_block
*nb
, unsigned long action
,
93 struct qcom_wdt
*wdt
= container_of(nb
, struct qcom_wdt
, restart_nb
);
97 * Trigger watchdog bite:
98 * Setup BITE_TIME to be 128ms, and enable WDT.
100 timeout
= 128 * wdt
->rate
/ 1000;
102 writel(0, wdt
->base
+ WDT_EN
);
103 writel(1, wdt
->base
+ WDT_RST
);
104 writel(timeout
, wdt
->base
+ WDT_BITE_TIME
);
105 writel(1, wdt
->base
+ WDT_EN
);
108 * Actually make sure the above sequence hits hardware before sleeping.
116 static int qcom_wdt_probe(struct platform_device
*pdev
)
118 struct qcom_wdt
*wdt
;
119 struct resource
*res
;
120 struct device_node
*np
= pdev
->dev
.of_node
;
124 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
128 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
130 /* We use CPU0's DGT for the watchdog */
131 if (of_property_read_u32(np
, "cpu-offset", &percpu_offset
))
134 res
->start
+= percpu_offset
;
135 res
->end
+= percpu_offset
;
137 wdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
138 if (IS_ERR(wdt
->base
))
139 return PTR_ERR(wdt
->base
);
141 wdt
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
142 if (IS_ERR(wdt
->clk
)) {
143 dev_err(&pdev
->dev
, "failed to get input clock\n");
144 return PTR_ERR(wdt
->clk
);
147 ret
= clk_prepare_enable(wdt
->clk
);
149 dev_err(&pdev
->dev
, "failed to setup clock\n");
154 * We use the clock rate to calculate the max timeout, so ensure it's
155 * not zero to avoid a divide-by-zero exception.
157 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
158 * that it would bite before a second elapses it's usefulness is
159 * limited. Bail if this is the case.
161 wdt
->rate
= clk_get_rate(wdt
->clk
);
162 if (wdt
->rate
== 0 ||
163 wdt
->rate
> 0x10000000U
) {
164 dev_err(&pdev
->dev
, "invalid clock rate\n");
166 goto err_clk_unprepare
;
169 wdt
->wdd
.dev
= &pdev
->dev
;
170 wdt
->wdd
.info
= &qcom_wdt_info
;
171 wdt
->wdd
.ops
= &qcom_wdt_ops
;
172 wdt
->wdd
.min_timeout
= 1;
173 wdt
->wdd
.max_timeout
= 0x10000000U
/ wdt
->rate
;
176 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
177 * default, unless the max timeout is less than 30 seconds, then use
180 wdt
->wdd
.timeout
= min(wdt
->wdd
.max_timeout
, 30U);
181 watchdog_init_timeout(&wdt
->wdd
, 0, &pdev
->dev
);
183 ret
= watchdog_register_device(&wdt
->wdd
);
185 dev_err(&pdev
->dev
, "failed to register watchdog\n");
186 goto err_clk_unprepare
;
190 * WDT restart notifier has priority 0 (use as a last resort)
192 wdt
->restart_nb
.notifier_call
= qcom_wdt_restart
;
193 ret
= register_restart_handler(&wdt
->restart_nb
);
195 dev_err(&pdev
->dev
, "failed to setup restart handler\n");
197 platform_set_drvdata(pdev
, wdt
);
201 clk_disable_unprepare(wdt
->clk
);
205 static int qcom_wdt_remove(struct platform_device
*pdev
)
207 struct qcom_wdt
*wdt
= platform_get_drvdata(pdev
);
209 unregister_restart_handler(&wdt
->restart_nb
);
210 watchdog_unregister_device(&wdt
->wdd
);
211 clk_disable_unprepare(wdt
->clk
);
215 static const struct of_device_id qcom_wdt_of_table
[] = {
216 { .compatible
= "qcom,kpss-timer" },
217 { .compatible
= "qcom,scss-timer" },
220 MODULE_DEVICE_TABLE(of
, qcom_wdt_of_table
);
222 static struct platform_driver qcom_watchdog_driver
= {
223 .probe
= qcom_wdt_probe
,
224 .remove
= qcom_wdt_remove
,
226 .name
= KBUILD_MODNAME
,
227 .of_match_table
= qcom_wdt_of_table
,
230 module_platform_driver(qcom_watchdog_driver
);
232 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
233 MODULE_LICENSE("GPL v2");