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 0x24
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
;
122 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
126 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
127 wdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
128 if (IS_ERR(wdt
->base
))
129 return PTR_ERR(wdt
->base
);
131 wdt
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
132 if (IS_ERR(wdt
->clk
)) {
133 dev_err(&pdev
->dev
, "failed to get input clock\n");
134 return PTR_ERR(wdt
->clk
);
137 ret
= clk_prepare_enable(wdt
->clk
);
139 dev_err(&pdev
->dev
, "failed to setup clock\n");
144 * We use the clock rate to calculate the max timeout, so ensure it's
145 * not zero to avoid a divide-by-zero exception.
147 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
148 * that it would bite before a second elapses it's usefulness is
149 * limited. Bail if this is the case.
151 wdt
->rate
= clk_get_rate(wdt
->clk
);
152 if (wdt
->rate
== 0 ||
153 wdt
->rate
> 0x10000000U
) {
154 dev_err(&pdev
->dev
, "invalid clock rate\n");
156 goto err_clk_unprepare
;
159 wdt
->wdd
.dev
= &pdev
->dev
;
160 wdt
->wdd
.info
= &qcom_wdt_info
;
161 wdt
->wdd
.ops
= &qcom_wdt_ops
;
162 wdt
->wdd
.min_timeout
= 1;
163 wdt
->wdd
.max_timeout
= 0x10000000U
/ wdt
->rate
;
166 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
167 * default, unless the max timeout is less than 30 seconds, then use
170 wdt
->wdd
.timeout
= min(wdt
->wdd
.max_timeout
, 30U);
171 watchdog_init_timeout(&wdt
->wdd
, 0, &pdev
->dev
);
173 ret
= watchdog_register_device(&wdt
->wdd
);
175 dev_err(&pdev
->dev
, "failed to register watchdog\n");
176 goto err_clk_unprepare
;
180 * WDT restart notifier has priority 0 (use as a last resort)
182 wdt
->restart_nb
.notifier_call
= qcom_wdt_restart
;
183 ret
= register_restart_handler(&wdt
->restart_nb
);
185 dev_err(&pdev
->dev
, "failed to setup restart handler\n");
187 platform_set_drvdata(pdev
, wdt
);
191 clk_disable_unprepare(wdt
->clk
);
195 static int qcom_wdt_remove(struct platform_device
*pdev
)
197 struct qcom_wdt
*wdt
= platform_get_drvdata(pdev
);
199 unregister_restart_handler(&wdt
->restart_nb
);
200 watchdog_unregister_device(&wdt
->wdd
);
201 clk_disable_unprepare(wdt
->clk
);
205 static const struct of_device_id qcom_wdt_of_table
[] = {
206 { .compatible
= "qcom,kpss-wdt-msm8960", },
207 { .compatible
= "qcom,kpss-wdt-apq8064", },
208 { .compatible
= "qcom,kpss-wdt-ipq8064", },
211 MODULE_DEVICE_TABLE(of
, qcom_wdt_of_table
);
213 static struct platform_driver qcom_watchdog_driver
= {
214 .probe
= qcom_wdt_probe
,
215 .remove
= qcom_wdt_remove
,
217 .name
= KBUILD_MODNAME
,
218 .of_match_table
= qcom_wdt_of_table
,
221 module_platform_driver(qcom_watchdog_driver
);
223 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
224 MODULE_LICENSE("GPL v2");