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/watchdog.h>
24 #define WDT_BITE_TIME 0x5C
27 struct watchdog_device wdd
;
34 struct qcom_wdt
*to_qcom_wdt(struct watchdog_device
*wdd
)
36 return container_of(wdd
, struct qcom_wdt
, wdd
);
39 static int qcom_wdt_start(struct watchdog_device
*wdd
)
41 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
43 writel(0, wdt
->base
+ WDT_EN
);
44 writel(1, wdt
->base
+ WDT_RST
);
45 writel(wdd
->timeout
* wdt
->rate
, wdt
->base
+ WDT_BITE_TIME
);
46 writel(1, wdt
->base
+ WDT_EN
);
50 static int qcom_wdt_stop(struct watchdog_device
*wdd
)
52 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
54 writel(0, wdt
->base
+ WDT_EN
);
58 static int qcom_wdt_ping(struct watchdog_device
*wdd
)
60 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
62 writel(1, wdt
->base
+ WDT_RST
);
66 static int qcom_wdt_set_timeout(struct watchdog_device
*wdd
,
69 wdd
->timeout
= timeout
;
70 return qcom_wdt_start(wdd
);
73 static int qcom_wdt_restart(struct watchdog_device
*wdd
, unsigned long action
,
76 struct qcom_wdt
*wdt
= to_qcom_wdt(wdd
);
80 * Trigger watchdog bite:
81 * Setup BITE_TIME to be 128ms, and enable WDT.
83 timeout
= 128 * wdt
->rate
/ 1000;
85 writel(0, wdt
->base
+ WDT_EN
);
86 writel(1, wdt
->base
+ WDT_RST
);
87 writel(timeout
, wdt
->base
+ WDT_BITE_TIME
);
88 writel(1, wdt
->base
+ WDT_EN
);
91 * Actually make sure the above sequence hits hardware before sleeping.
99 static const struct watchdog_ops qcom_wdt_ops
= {
100 .start
= qcom_wdt_start
,
101 .stop
= qcom_wdt_stop
,
102 .ping
= qcom_wdt_ping
,
103 .set_timeout
= qcom_wdt_set_timeout
,
104 .restart
= qcom_wdt_restart
,
105 .owner
= THIS_MODULE
,
108 static const struct watchdog_info qcom_wdt_info
= {
109 .options
= WDIOF_KEEPALIVEPING
112 .identity
= KBUILD_MODNAME
,
115 static int qcom_wdt_probe(struct platform_device
*pdev
)
117 struct qcom_wdt
*wdt
;
118 struct resource
*res
;
119 struct device_node
*np
= pdev
->dev
.of_node
;
123 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(*wdt
), GFP_KERNEL
);
127 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
129 /* We use CPU0's DGT for the watchdog */
130 if (of_property_read_u32(np
, "cpu-offset", &percpu_offset
))
133 res
->start
+= percpu_offset
;
134 res
->end
+= percpu_offset
;
136 wdt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
137 if (IS_ERR(wdt
->base
))
138 return PTR_ERR(wdt
->base
);
140 wdt
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
141 if (IS_ERR(wdt
->clk
)) {
142 dev_err(&pdev
->dev
, "failed to get input clock\n");
143 return PTR_ERR(wdt
->clk
);
146 ret
= clk_prepare_enable(wdt
->clk
);
148 dev_err(&pdev
->dev
, "failed to setup clock\n");
153 * We use the clock rate to calculate the max timeout, so ensure it's
154 * not zero to avoid a divide-by-zero exception.
156 * WATCHDOG_CORE assumes units of seconds, if the WDT is clocked such
157 * that it would bite before a second elapses it's usefulness is
158 * limited. Bail if this is the case.
160 wdt
->rate
= clk_get_rate(wdt
->clk
);
161 if (wdt
->rate
== 0 ||
162 wdt
->rate
> 0x10000000U
) {
163 dev_err(&pdev
->dev
, "invalid clock rate\n");
165 goto err_clk_unprepare
;
168 wdt
->wdd
.info
= &qcom_wdt_info
;
169 wdt
->wdd
.ops
= &qcom_wdt_ops
;
170 wdt
->wdd
.min_timeout
= 1;
171 wdt
->wdd
.max_timeout
= 0x10000000U
/ wdt
->rate
;
172 wdt
->wdd
.parent
= &pdev
->dev
;
175 * If 'timeout-sec' unspecified in devicetree, assume a 30 second
176 * default, unless the max timeout is less than 30 seconds, then use
179 wdt
->wdd
.timeout
= min(wdt
->wdd
.max_timeout
, 30U);
180 watchdog_init_timeout(&wdt
->wdd
, 0, &pdev
->dev
);
182 ret
= watchdog_register_device(&wdt
->wdd
);
184 dev_err(&pdev
->dev
, "failed to register watchdog\n");
185 goto err_clk_unprepare
;
188 platform_set_drvdata(pdev
, wdt
);
192 clk_disable_unprepare(wdt
->clk
);
196 static int qcom_wdt_remove(struct platform_device
*pdev
)
198 struct qcom_wdt
*wdt
= platform_get_drvdata(pdev
);
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-timer" },
207 { .compatible
= "qcom,scss-timer" },
210 MODULE_DEVICE_TABLE(of
, qcom_wdt_of_table
);
212 static struct platform_driver qcom_watchdog_driver
= {
213 .probe
= qcom_wdt_probe
,
214 .remove
= qcom_wdt_remove
,
216 .name
= KBUILD_MODNAME
,
217 .of_match_table
= qcom_wdt_of_table
,
220 module_platform_driver(qcom_watchdog_driver
);
222 MODULE_DESCRIPTION("QCOM KPSS Watchdog Driver");
223 MODULE_LICENSE("GPL v2");